DDC-839: Error when trying to update PersistentCollection #1035

Closed
opened 2026-01-22 12:59:34 +01:00 by admin · 5 comments
Owner

Originally created by @doctrinebot on GitHub (Oct 15, 2010).

Originally assigned to: @beberlei on GitHub.

Jira issue originally created by user vigor_bg:

This is the error that i get :
"Argument 1 passed to Doctrine\ORM\UnitOfWork::getCollectionPersister() must be an array, null given, called in /home/vis/projects/CRM/trunk/library/Doctrine/ORM/UnitOfWork.php on line 302 and defined at /home/vis/projects/CRM/trunk/library/Doctrine/ORM/UnitOfWork.php:2149"

This happens when i try to update Entity that have PersistentCollection but when i was creating the entity did not create entities to be attached to it by the PersistentCollection. Later on i called it and and used :

$collectionEntity = $em->getRepository('CollectionEntityName')->findOneBy($data);
$entity                   = $em->getRepository('EntityName')->findOneBy($entityData);
$entity->getCollection()->add($collectionEntity);
$em->presist();
$em->flush();
Originally created by @doctrinebot on GitHub (Oct 15, 2010). Originally assigned to: @beberlei on GitHub. Jira issue originally created by user vigor_bg: This is the error that i get : "Argument 1 passed to Doctrine\ORM\UnitOfWork::getCollectionPersister() must be an array, null given, called in /home/vis/projects/CRM/trunk/library/Doctrine/ORM/UnitOfWork.php on line 302 and defined at /home/vis/projects/CRM/trunk/library/Doctrine/ORM/UnitOfWork.php:2149" This happens when i try to update Entity that have PersistentCollection but when i was creating the entity did not create entities to be attached to it by the PersistentCollection. Later on i called it and and used : ``` $collectionEntity = $em->getRepository('CollectionEntityName')->findOneBy($data); $entity = $em->getRepository('EntityName')->findOneBy($entityData); $entity->getCollection()->add($collectionEntity); $em->presist(); $em->flush(); ```
admin added the Bug label 2026-01-22 12:59:34 +01:00
admin closed this issue 2026-01-22 12:59:34 +01:00
Author
Owner

@doctrinebot commented on GitHub (Oct 15, 2010):

@doctrinebot commented on GitHub (Oct 15, 2010): - duplicates [DDC-767: Updating many-to-many relations](http://www.doctrine-project.org/jira/browse/DDC-767)
Author
Owner

@doctrinebot commented on GitHub (Oct 15, 2010):

Comment created by vigor_bg:

After some investigating i did find a solution that seems to work.
In Doctrine\ORM\UnitOfWork;
in the function computeChangeSet()

from line 482

 // Look for changes in associations of the entity
        foreach ($class->associationMappings as $field => $assoc) {
            $val = $class->reflFields[$field]->getValue($entity);
            if ($val !== null) {

            /****
                 * Fix done by Victor on 15.10.2010
                 */
                if ($val instanceof PersistentCollection) {
                    $val->setOwner($entity, $assoc);
                }
                /****
                 * End Fix
                 */

                $this->computeAssociationChanges($assoc, $val);

            }
        }

That way i make sure that the owner for the PersistentCollection is set for sure and when it is called in the commit function it does not give errors.

Hope that this solution will work for you. :)

@doctrinebot commented on GitHub (Oct 15, 2010): Comment created by vigor_bg: After some investigating i did find a solution that seems to work. In Doctrine\ORM\UnitOfWork; in the function computeChangeSet() from line 482 ``` // Look for changes in associations of the entity foreach ($class->associationMappings as $field => $assoc) { $val = $class->reflFields[$field]->getValue($entity); if ($val !== null) { /**** * Fix done by Victor on 15.10.2010 */ if ($val instanceof PersistentCollection) { $val->setOwner($entity, $assoc); } /**** * End Fix */ $this->computeAssociationChanges($assoc, $val); } } ``` That way i make sure that the owner for the PersistentCollection is set for sure and when it is called in the commit function it does not give errors. Hope that this solution will work for you. :)
Author
Owner

@doctrinebot commented on GitHub (Oct 28, 2010):

Comment created by seldaek:

I hit the same issue, if a many-to-many's collection is empty (either at the firstr creation or if you clear()'d it later on), and then you add() something to it and try to flush, it does not know its parent.

Victor's fix has also been verified here. Not sure if it's the best way, but it's working. Please fix this asap because it's essentially preventing relations to be used for me. I had to resort to a ugly hack for the time being to always have one item in the association that is hidden..

@doctrinebot commented on GitHub (Oct 28, 2010): Comment created by seldaek: I hit the same issue, if a many-to-many's collection is empty (either at the firstr creation or if you clear()'d it later on), and then you add() something to it and try to flush, it does not know its parent. Victor's fix has also been verified here. Not sure if it's the best way, but it's working. Please fix this asap because it's essentially preventing relations to be used for me. I had to resort to a ugly hack for the time being to always have one item in the association that is hidden..
Author
Owner

@doctrinebot commented on GitHub (Oct 29, 2010):

Comment created by @beberlei:

Fixed

@doctrinebot commented on GitHub (Oct 29, 2010): Comment created by @beberlei: Fixed
Author
Owner

@doctrinebot commented on GitHub (Oct 29, 2010):

Issue was closed with resolution "Fixed"

@doctrinebot commented on GitHub (Oct 29, 2010): Issue was closed with resolution "Fixed"
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#1035