Symfony2: How to remove an element from a Doctrine ArrayCollection (many-to-many relation)? Symfony2: How to remove an element from a Doctrine ArrayCollection (many-to-many relation)? symfony symfony

Symfony2: How to remove an element from a Doctrine ArrayCollection (many-to-many relation)?


You're looking for the ArrayCollection::removeElement method here.

public function removeSchema(SchemaInterface $schema){    $this->schemata->removeElement($schema)    return $this;}

tip:

You can use ArrayCollection::add to add elements to an existing collection. OOP.

In some cases you may also want to check wether already contains the element before adding it.

public function addSchema(SchemaInterface $schema){    if (!$this->schemata->contains($schema)) {        $this->schemata->add($schema);    }    return $this;}