I have two tables with a ManyToMany relationship. I needed to add some extra fields to this relationship, so I created a new entity that has a ManyToOne relationship with each of the two tables. The issue I'm having is with removing the relationship entity. I don't want to remove entries in either of the two tables, I just want to remove the relationship. What I've done to set up the relating entity is pretty much described here:
Doctrine2: Best way to handle many-to-many with extra columns in reference table
To remove the relationship, I've tried removing the relating element from each of the two other table entries, and then setting the reference to those tables to null in the relating entity. That doesn't seem to work; Doctrine tries to execute a query to update the entry in the relating table and set both the foreign keys to null. I would expect it to remove the entry in the relating table, if all references to it have been removed.
Let me know if you'd like to see my entities, or if this makes enough sense as it stands.
Actually, deleting the entity that acts as "join-table" does the trick.
Nothing should be cascaded.
So assuming you have a relation like
User <- UserGroup -> Group
You just need to remove the UserGroup entity.
If you remove User or Group, and you have set cascade persist operations correctly, also UserGroup will go. You can also use orphanRemoval to avoid having UserGroup assigned to different User or Group elements.
Related
I want to add a logging system to my Symfony backend to keep track of who does that. I will need a relation between my logg entity and my users entity.
From what i understand, foreign keys work in two ways, either prevent deletion of an entry if it is linked to another table item or cascade to remove orphans.
Is there a way to have a relation between two tables but keep the orphans ids in the table ?
Thanks
You could use a "SoftDelete", no ?
Add a property deletedAt that you update on remove event and when you query your relation you get only entities that have deletedAt IS NULL.
That way you keep the relations so you know which entities were related, etc.
I think there are some Bundles that does the SoftDelete thing.
I have 2 Doctrine Entities with many-to-many relations. When I edit the first entity I want to be able to select the checkboxes that have the data from the 2nd entity to establish the joins for particular entry.
It works fine on creating a new Entry (using Array Collection), but when I want to edit an Entry - it adds the ones that I have selected without removing the previous choice (unchecking).
Which way would be the correct way to do that and how?
Remove all the Join table data for the Entry that is being updated,
then set the new data. (How can I remove it from the join table that
is not an Entity?)
Pass all the data from the 2nd Entity and remove
those that aren't checked (seems super-clumsy?)
Some other way I am not aware of?
I am not using Symfony, just Doctrine.
Doctrine makes working with the many-to-many associations quite easy. Your associations are stored into an ArrayCollection class that has some methods that can help you. First of all, check all the available methods for the ArrayCollection here (Doctrine API - ArrayCollection)
In your case, I'd use this approach: use the clear method on your ArrayCollection that contains the relationship with the 2nd entity and populate it again with the checked elements. After this, call the flush method on the entitymanager.
Another approach consists in filtering your collection (with the filter method) for getting a brand new ArrayCollection that contains only the elements that are checked. Like the first approach, associate this new collection to the relationship's ArrayCollection and call the flush method on the entitymanager.
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 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.
Tables:
tbl_user(id, name)
tbl_group(id, name);
tbl_user_group(user_id, group_id);
If there were all three classes generated User,Group,UserGroup. How should i setup the relations in User or Group so that they are mapped through the user_group table?
I'm using Doctrine 1.2
It's up to you to define their associations, purely because only you will know how you want your application to work. Can a user have more than one group? Only one? If a group is deleted, will all associated users be deleted? These are the questions you need to ask yourself.
If you follow the example on Doctrine's documentation for Many-to-Many relationships (which coincidentally covers exactly what you need), you'll get there.