Godineau Félicie
Godineau Félicie

Reputation: 438

Add ArrayCollection of Entities in the same entity type

Is it possible to keep an array of entities A in the entity A ? How to do this with Doctrine ?

I have :

class A {
   /**
     * @var \Doctrine\Common\Collections\ArrayCollection
     */
    private $sisters;
}

But I don't know what to add to have Doctrine do what I need.

Upvotes: 1

Views: 677

Answers (1)

rafrsr
rafrsr

Reputation: 2030

A can have many sisters, and many sisters can be sister of A (Many-To-Many, Self-referencing):

/**
 * @ORM\Entity()
 * @ORM\Table()
 */
class A
{
    /**
     * @var integer
     *
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    protected $id;

    /**
     * @var \Doctrine\Common\Collections\ArrayCollection
     * @ORM\ManyToMany(targetEntity="AppBundle\Entity\A")
     */
    private $sisters;
}

http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/association-mapping.html#many-to-many-self-referencing

Upvotes: 2

Related Questions