Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>Solution</h2> <p>What you're looking for is <a href="http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/working-with-associations.html#orphan-removal">orphan removal</a>.</p> <p>You can read on if you'd like details on why your current situation isn't working.</p> <h2>Cascade woes</h2> <p>The cascade operation won't do what you want unfortunately. The "cascade=[remove]" just means that if the entity object is removed then doctrine will loop through and remove all child attributes as well:</p> <pre><code>$em-&gt;remove($entity); // doctrine will basically do the following automatically foreach ($entity-&gt;getAttributes() as $attr) { $em-&gt;remove($attr); } </code></pre> <h2>How to do it manually</h2> <p>If you needed to remove an attribute from an entity you'd delete the attribute like so:</p> <pre><code>$entity-&gt;getAttributes()-&gt;removeElement($attr); $em-&gt;remove($attribute); </code></pre> <h2>Solution details</h2> <p>But, to do that automatically we use the orphan removal option. We simply tell doctrine that attributes can only belong to entities, and if an attribute no longer belongs to an entity, simply delete it:</p> <pre><code>/** * @ORM\OneToMany(targetEntity="Attribute", mappedBy="entity", orphanRemoval=true, cascade={"persist", "remove", "merge"}) **/ protected $attributes; </code></pre> <p>Then, you can remove the attribute by simply doing this:</p> <pre><code>$entity-&gt;getAttributes()-&gt;removeElement($attr); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload