You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When a field is a toMany relation, Doctrine relies on a special Collection implementation to track changes.
A common mistake is a write a setter that replaces the collection entirely instead of writing into the existing collection. This will make things that your collection field went from an empty list to its new content when checking for changes (as it lost all tracking for the existing content), which will do very bad things for the database data (in the best case, it triggers an error due to duplicate data in a unique index. In the worse case, it silently corrupts your data).
It would be great if phpstan-doctrine could prevent such mistakes for people using it.
The text was updated successfully, but these errors were encountered:
@VincentLanglet your suggested code would indeed work (modulo the wrong variable name used in the snippet, which would be caught by phpstan), but would still not be optimal if some items are both in the existing collection and the new one as you would still delete them from the join table for clearing and then adding them again.
The best implementation is to compare both collections, to add the missing items and remove the extra items.
When a field is a toMany relation, Doctrine relies on a special Collection implementation to track changes.
A common mistake is a write a setter that replaces the collection entirely instead of writing into the existing collection. This will make things that your collection field went from an empty list to its new content when checking for changes (as it lost all tracking for the existing content), which will do very bad things for the database data (in the best case, it triggers an error due to duplicate data in a unique index. In the worse case, it silently corrupts your data).
It would be great if phpstan-doctrine could prevent such mistakes for people using it.
The text was updated successfully, but these errors were encountered: