Doctrine2 ManyToMany Self referencing

Asked
Active3 hr before
Viewed126 times

7 Answers

manytomany
90%

I got problems with persisting many to many self referencing relations. I receive error: , Meta Stack Overflow ,Stack Overflow en español,Stack Overflow em Português

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

//$collection->clear();
88%

You can define a self-referencing one-to-one relationships like below.,ManyToOne - Many instances of the current Entity refer to One instance of the referred Entity.,OneToOne - One instance of the current Entity refers to One instance of the referred Entity.,OneToMany - One instance of the current Entity has Many instances (references) to the referred Entity.

1 < ? php
/** @Entity */
class User {
   // ...

   /**
    * @ManyToOne(targetEntity="Address")
    * @JoinColumn(name="address_id", referencedColumnName="id")
    */
   private $address;
}

/** @Entity */
class Address {
   // ...
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
load more v
72%

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,A quick fix to this problem is to comment the following line in the MergeDoctrineCollectionListener class:,If you're testing with Symfony2 forms remember to set

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;
}
load more v
65%

More on one-to-many/many-to-one associations in Doctrine 2,By default, self-referencing ManyToMany relationships under Doctrine involve an owning side and an inverse side, as explained in the documentation.,A simple approach would be to use a unidirectional ManyToMany association, and treat it as if it where a bidirectional one (keeping both sides in sync):,That's really easier than trying to handle this with many to many relations methods. I would do that if I had to do what you need (and actually I'm doing so).

Following the example in the docs:

< ? php
/** @Entity **/
class User {
   // ...

   /**
    * @ManyToMany(targetEntity="User")
    **/
   private $friends;

   public
   function __construct() {
      $this - > friends = new DoctrineCommonCollectionsArrayCollection();
   }

   // ...
}
load more v
75%

Change use Doctrine\Common\Collections\ArrayCollection; for use Doctrine\Common\Collections\Collection;,"$this->userGroups = new ArrayCollection();" ...,@Ocramius is there a correct way to set the users from a Collection? we have somethign like:,and the methods: public function getUser() or getUserGroups(), for example, return a array() and not a PersistentCollection.

@Ocramius
No, this not Fixed, this is infinite loop !!

 "addUser" => "addUserGroup" => "addUser" => "addUserGroup" => "addUser" => ...
load more v
40%

load more v
22%

By default, self-referencing ManyToMany relationships under Doctrine involve an owning side and an inverse side, as explained in the documentation.,A simple approach would be to use a unidirectional ManyToMany association, and treat it as if it where a bidirectional one (keeping both sides in sync):,If you need a system where a user can add a friend, but that friend has to confirm that they are indeed friends, you'll need to store that confirmation in the join-table. You then no longer have a ManyToMany association, but something like User <- OneToMany -> Friendship <- ManyToOne -> User., How to solve Code First from database many-to-many mapping in Entity Framework? Bellow code snippet showing my scenario: Now within #code# EF Generate code like bellow now this is fine add / insert data into #code# table. But how to Get /... iheaney · saved on 1 month ago

Following the example in the docs:

< ? php
/** @Entity **/
class User {
   // ...

   /**
    * @ManyToMany(targetEntity="User")
    **/
   private $friends;

   public
   function __construct() {
      $this - > friends = new DoctrineCommonCollectionsArrayCollection();
   }

   // ...
}
load more v

Other "manytomany-undefined" queries related to "Doctrine2 ManyToMany Self referencing"