Skip to content

Commit 042709a

Browse files
committed
minor #18828 Update associations.rst (fishbone1)
This PR was submitted for the 6.4 branch but it was merged into the 5.4 branch instead. Discussion ---------- Update associations.rst Added caution block that informs about the risks of generated convenience "add" and "remove" methods on the inverse side. We wasted hours for debugging an "Out of memory" error. In our case we have emails that are connected to users who can access those emails (many-to-many). There is a very large amount of emails. The memory was exceeded when we called `$email->addUser($user)` because the generated code checked if the user already has that email. <!-- If your pull request fixes a BUG, use the oldest maintained branch that contains the bug (see https://symfony.com/releases for the list of maintained branches). If your pull request documents a NEW FEATURE, use the same Symfony branch where the feature was introduced (and `6.x` for features of unreleased versions). --> Commits ------- 2f88c8c Update associations.rst
2 parents a31f282 + 2f88c8c commit 042709a

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

doctrine/associations.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,15 @@ also generated a ``removeProduct()`` method::
644644
Thanks to this, if you call ``$category->removeProduct($product)``, the ``category_id``
645645
on that ``Product`` will be set to ``null`` in the database.
646646

647+
.. warning::
648+
649+
Please be aware that the inverse side could be associated with a large amount of records.
650+
I.e. there could be a large amount of products with the same category.
651+
In this case ``$this->products->contains($product)`` could lead to unwanted database
652+
requests and very high memory consumption with the risk of hard to debug "Out of memory" errors.
653+
654+
So make sure if you need an inverse side and check if the generated code could lead to such issues.
655+
647656
But, instead of setting the ``category_id`` to null, what if you want the ``Product``
648657
to be *deleted* if it becomes "orphaned" (i.e. without a ``Category``)? To choose
649658
that behavior, use the `orphanRemoval`_ option inside ``Category``:

0 commit comments

Comments
 (0)