I read about some arguments on Soft delete vs. Archive table which led me to this question.
I thought I could implement an entity Product which is mapped to two identical tables Product and ArchivedProduct, but there is a known limitation of Doctrine 2
Mapping many tables to one entity
It is not possible to map several equally looking tables onto one entity. For example, if you have a production and an archive table of a certain business concept then you cannot have both tables map to the same entity.
So I thought I could use two entities, Product and ArchivedProduct, that are mapped to the same table, but it led to another problem that the One-to-Many reference $products in the entity Category should be mapped to two different entities.
Should I use Inheritance Mapping on Product and ArchivedProduct? What's the best practice of Archive table with Symfony 4 and Doctrine 2?
Related
My database diagram corresponds to:
Each table has his own Symfony Entity class. My app view shows:
I need to associate a Discount Entity to multiple entities: Sneaker, Tshirt, Trouser or even more entities.
First solution:
Create N:N tables between discounts and the others. The problem is that I could need create new tables to apply discount and then I would have to create more N:N tables. So I discard this solution.
Second solution:
Create a generic table with to_entity and to_entity_id fields that let me map discount to to_entity entity destination (Sneaker, Tshirt, Trouser or other):
How can I implement second solution in Symfony ? Or is there another solution possible ?
Thanks.
Polymorphism with Doctrine is not easy at all.
Try to solve it through mapped superclasses and/or inheritance. Your first solution could be simplified by using single-table inheritance. You should change the discriminator map for every new "discountable" entity.
Investigate on Dynamic mapping through loadClassMetadata event. You can create OneToMany relationships for every entity implementing a custom interface (i.e. DiscountableInterface).
Introduction
I'm working on a symfony3 project and I have a lot of different "types" for certain entities. What I mean by this is that, I have a TypeUser, TypeEvent, TypeMeeting tables.
These tables will only have an id and a label.
Problem:
When using doctrine, I can link 1 entity to 1 table using the anotations, like so:
/**
* #ORM\Entity
* #ORM\Table(name="TypeUser")
*/
However, I would like this part to be completely generic. How can I specify the Table name depending on the type I need?
Is there another alternative when using Doctrine other than the annotations to make this possible?
I would really like to avoid making n entities for n tables, when they are very similar in name and in structure.
Question:
Is it possible to make one generic entity to match a specific TypeXXXX table, to reduce redundancy? If so how would I go about doing it?
Similar Doctrine 2.1 - Map entity to multiple tables
Symfony book on doctrine
I have three tables:
banners (id, url, img)
banner_on_position (banner, position, loading)
banner_positions (id, name)
When I run the doctrine:mapping:import, doctrine creates only two tables. Banners and BannerPositions. Auto-generation doesn't create an own entity for the banner_on_position table.
Is there a way to access the loading attribute with querybuilder?
OR
How to tell doctrine to create own entity?
Until now I had no problems with auto generation of entities and it was very comfortable. I hope I don't have to create to code manually.
You have to create code manually in that case. If your many_to_many table have some extra properties then you need to use that trick: Both side need to use oneToMany relation for banner_on_position and banner_on_position need to have manyToOne to both tables.
Check how we did it in similar case (ContainerWidget have link to container, widget and custom parameter - position). https://github.com/superdesk/web-publisher/blob/master/src/SWP/Bundle/TemplateEngineBundle/Resources/config/doctrine/ContainerWidget.orm.yml
Here is also really good question and answers on stackoverflow: Doctrine2: Best way to handle many-to-many with extra columns in reference table
I'm using the most recent versions of ZF2 and doctrine2 orm in a project.
What I want to achieve, is a generic class "Comment" that has a relation to another entity. This entity could be from various classes "Post", "Image" aso., depending on what the comment should be related to.
The idea was to have one table for all comments and a column that defines the type of related entity, just like using the discriminator mapping.
Problem with the discriminator is that I have to create different classes using extends, for every use case. This does not seem efficient as I will have to create quite a lot of subclasses to reflect all relations needed.
I would like to use just the base class of Comment and have one or two column(s) defining the relation like "Post:25" (target class and id in one column) or "Post" | "25" (target class an id in separate columns).
Is this possible in doctrine or is there a besser way of dealing with this?
I'm trying to figure out the correct way to map my three entities together. I have a "HouseType", a "Resource" and a HouseType can have multiple Resources (with an integer indicating how many of that resource they have). So I have three tables, the house_type, resource and house_type_resource (with house_type_id, resource_id, and num).
I can't figure out what type of association mapping I should do. I'm reading this page: http://docs.doctrine-project.org/en/latest/reference/association-mapping.html but I don't see one that's like mine, where I have a third entity to represent the join table, because it has a specific property that needs to be included (the num). It's sort of like the "Many-To-Many, Unidirectional" but I need that third Entity mapped in there. I see the note at the end about doing an Association class but I don't see any more info on that.
Once you start adding extra columns, Doctrine stops treating it as a pure many-to-many relationship because there is extra data, and this data should be managed with an entity. You should create a new entity called HouseTypeResource, then create one-to-many relationships with this inside your HouseType and Resource entities.