Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove element from ArrayCollection on OneToMany wont delete it on persist()?

Tags:

doctrine-orm

In a Zend Framework 2 project, we got two Doctrine 2 entities and we would like to remove an element from a collection allready saved in the database...

So we have a first entity named FormGroupConstraint :

/**
 * FormGroupConstraint
 *
 * @ORM\Table(name="form_group_constraint")
 * @ORM\Entity(repositoryClass="Application\Dao\FormGroupConstraintDao")
 */
class FormGroupConstraint {

    /**
     * @var integer 
     * 
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
     protected $id;

    /**
     * @param \Doctrine\Common\Collections\ArrayCollection
     * @ORM\OneToMany(targetEntity="Application\Entity\FormQuestionConstraint", mappedBy="groupConstraint", fetch="EAGER", cascade={"persist", "merge", "refresh", "remove"})
     */
     protected $constraints;

     public function __construct() 
          $this->constraints = new \Doctrine\Common\Collections\ArrayCollection();
     }

     /**
     * @param \Doctrine\Common\Collections\ArrayCollection $constraints
     */
     public function addConstraints($constraints) {
        foreach ($constraints as $constraint) {
            $this->constraints->add($constraint);
        }
        return $this->constraints;
    }
    /**
     * @param \Doctrine\Common\Collections\ArrayCollection $constraints
     */
     public function removeConstraints($constraintsToRemove) {
            foreach ($constraintsToRemove as $key => $constraintToRemove) {
                $this->constraints->removeElement($constraintToRemove);
            }
            return $this->constraints;
     }
}

And the sub entity named FormQuestionConstraint :

/**
 * FormQuestionConstraint
 *
 * @ORM\Table(name="form_question_constraint")
 * @ORM\Entity(repositoryClass="Application\Dao\FormQuestionConstraintDao")
 */
class FormQuestionConstraint
{
    /**
    * @var integer
    *
    * @ORM\Column(name="id", type="integer", nullable=false)
    * @ORM\Id
    * @ORM\GeneratedValue(strategy="IDENTITY")
    */
    protected $id;

    /**
    * @var \Application\Entity\FormGroupConstraint
    *
    * @ORM\ManyToOne(targetEntity="Application\Entity\FormGroupConstraint", cascade=   {"persist", "merge", "refresh", "remove"})
    * @ORM\JoinColumns({
    *   @ORM\JoinColumn(name="form_group_constraint_id", referencedColumnName="id")
    * })
    */
    protected $groupConstraint;
}

So if we try to create, persist, flush a FormGroupConstraint entity, no problem, but when we want to remove an element of $constraints ArrayCollection, nothing happens...

We are using doctrine-orm-module for zend 2 installed with composer.phar in dev-master...

Here is an example of what we are trying to do :

$constraint = $this->findConstraintByGroup(1);

$formGroupConstraint = $this->_em->findOneById(1);

$formGroupConstraint->getConstraints()->removeElement($constraint);
$this->_em->persist($formGroupConstraint);
$this->_em->flush();

No error, but no delete or remove... And if we var_dump() the getConstraints() before persist(), actually the element is still in the ArrayCollection...

Could anyone explain us how we could do that or why the element is not removed ?

like image 755
Axel Agarrat Avatar asked Apr 19 '13 17:04

Axel Agarrat


1 Answers

Maybe a bit late but try adding orphanRemoval=true to the inverse side (OneToMany) of relationship

@ORM\OneToMany(
    targetEntity="Application\Entity\FormQuestionConstraint",
    mappedBy="groupConstraint",
    cascade={"persist", "remove"},
    orphanRemoval=true
)
like image 105
olegkhuss Avatar answered Sep 22 '22 12:09

olegkhuss