Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doctrine2 ManyToMany Self referencing

I got problems with persisting many to many self referencing relations. I receive error:

The class 'Doctrine\ORM\Persisters\ManyToManyPersister' was not found in the chain configured namespaces

This happens when I remove all children form item saved with them. Leaving at least one don't make error happen. Also if I initially save entity with no children everything works fine.

/**
  * West\AlbumBundle\Entity\Album
  *
  * @ORM\Table(name="albums")
  * @ORM\Entity(repositoryClass="West\AlbumBundle\Entity\AlbumRepository")
  * @ORM\HasLifecycleCallbacks
  */
 class Album extends Entity implements CrudEntity
 {

     /**
      * @ORM\ManyToMany(targetEntity="Album")
      * @ORM\JoinTable(name="albums_relations",
      *         joinColumns={@ORM\JoinColumn(name="album_id", referencedColumnName="id")},
      *         inverseJoinColumns={@ORM\JoinColumn(name="related_album_id", referencedColumnName="id")}
      * ) 
      * @var ArrayCollection
      */
      protected $related_albums;
}

If you're testing with Symfony2 forms remember to set

"by_reference" => false

like image 456
Maciej Pyszyński Avatar asked Aug 22 '12 16:08

Maciej Pyszyński


1 Answers

I've found that the problem happens when the method UnitOfWork.scheduleCollectionDeletion is called, for example, from MergeDoctrineCollectionListener.onBind() and the PersistentCollection object has been cloned ( 'by_reference' = false )

A quick fix to this problem is to comment the following line in the MergeDoctrineCollectionListener class:

//$collection->clear();
like image 124
dagaren Avatar answered Sep 26 '22 19:09

dagaren