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.
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).
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
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.
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.