mirror of
https://github.com/doctrine/orm.git
synced 2026-03-24 06:52:09 +01:00
One-To-Many Bidirectional: Bug in doctrine:generate:entities or in docs #5738
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @ThomasLandauer on GitHub (Oct 11, 2017).
Somehting is wrong in the following scenario - I'm just not sure if it's a bug in
doctrine:generate:entitiesor in the docs ;-)When I set up two entities as in this example: http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/association-mapping.html#one-to-many-bidirectional
...and then run
doctrine:generate:entities, I get this inProduct.php:However, when I do
$product->addFeature($feature);, the Feature is not persisted correctly: The fieldfeature.product_idis not populated.This is in accordance with the docs (same link as above):
So the question is: Why is
public function addFeaturegenerated at all, if it's useless??Or am I doing it wrong and it isn't useless?
@mateuszsip commented on GitHub (Oct 14, 2017):
Think that this section will explain it.
@ThomasLandauer commented on GitHub (Oct 14, 2017):
@kejwmen Yeah, I know that section, cause I wrote it ;-) https://github.com/doctrine/doctrine2/pull/6219
If that's the only reason why
addFeature()is generated automatically, then I'd say it should only be created ifcascade:persistis set.@mateuszsip commented on GitHub (Oct 14, 2017):
;) Nice work.
In my opinion it should, you always have a choice of cascade vs manual persist/remove.
Sometimes you need cascade (to manage entire aggregate) sometimes it could be dangerous (relations between aggregates).
And lack of
cascade:persistdoesn't imply you don't needaddFeature.The same thing about
cascade:removeandremoveFeature.Maybe docs related to entity generator should link to that (working with associations) section?
@ThomasLandauer commented on GitHub (Oct 14, 2017):
Why??
That's exactly the point I'm looking for! What's the purpose of
addFeature(), if it isn't persisted in this case?@lcobucci commented on GitHub (Oct 29, 2017):
@ThomasLandauer
addFeature()can also be used with objects that were previously persisted. Like:The idea is just to associate entities, how to persist them should be something consciously designed by the developer. And we should use cascade operations with caution, since they add extra work to the UoW, which may impact on the performance of the software.
I hope this helps you to understand =)
@ThomasLandauer commented on GitHub (Oct 29, 2017):
Well, OK, thanks!
So the bottom line is: It does make sense somehow, but only in these two cases, which both are rare:
cascade:persistadded entity is already persistedThis should be explained in the docs somewhere! I justed looked around at http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/working-with-associations.html but couldn't find an appropriate place. I didn't even find a decent explanation for those
add*-functions - so probably that would be the first thing to do....@mateuszsip commented on GitHub (Oct 29, 2017):
Thanks @lcobucci and sorry @ThomasLandauer - I forgot about you :(