title: Doctrine one to many detach from array collection
body: <p>I have entities Campaign and Creative with relations One Campaign has Many Creatives</p>
  
  <p><strong>In Campaign:</strong> </p>
  
  <pre><code>/**
   * @ORM\OneToMany(targetEntity="Creative", mappedBy="campaign", cascade={"detach", "persist"})
   */
  protected $creatives;
  </code></pre>
  
  <p><strong>In Creative:</strong> </p>
  
  <pre><code>/**
   * @ORM\ManyToOne(targetEntity="Campaign", inversedBy="creatives", cascade={"detach", "persist"})
   * @ORM\JoinColumn(name="campaign_id", referencedColumnName="id")
   */
  private $campaign;
  </code></pre>
  
  <p>In frontend, I have multiple select where I can add/ remove creatives, and it sends an array of creative ids to the backend.</p>
  
  <p>Problem is how to detach Creative from Campaign when I removing one from select. </p>
  
  <p>I did not found any solution what doctrine can propose, so I tried:</p>
  
  <pre><code>$campaign-&gt;getCreatives()-&gt;clear();
  foreach (json_decode($data['creatives'],true) as $creativeId) {
      $campaign-&gt;addCreative($this-&gt;_em-&gt;getReference(Creative::class, $creativeId));
  }
  </code></pre>
  
  <p>but looks like it's not working, code removing the element from Campaign object but not changing <code>campaign_id</code> to null for related creative in the database. Maybe I need also detach it from Creative side, but it's extra work to do, I thought that cascade={"detach"} should handle it. </p>
  
  <p>I would be grateful for any help!!!</p>
  
  <p>FYI: I'm using doctrine with Laravel.</p>
  
  <p><strong>Updated</strong>
  I created sync method in Campaign as a workaround:</p>
  
  <pre><code>public function syncCreatives(array $creatives)
  {
      //detach all creatives
      if(empty($creatives)) {
          /** @var Creative $creative */
          foreach ($this-&gt;creatives-&gt;getIterator() as $creative) {
              $creative-&gt;setCampaign(null);
          }
          $this-&gt;creatives-&gt;clear();
  
          return true;
      }
      //get nonexistent creatives
      $creativesToRemove = $this-&gt;creatives-&gt;filter(function (Creative $creative) use (&amp;$creatives) {
          if(isset($creatives[$creative-&gt;getId()])) {
              unset($creatives[$creative-&gt;getId()]);
  
              return false;
          }
  
          return true;
      });
      //remove nonexistent creatives
      foreach ($creativesToRemove-&gt;getIterator() as $creative) {
          $this-&gt;removeCreative($creative);
      }
      //add new creatives
      foreach ($creatives as $creative) {
          $this-&gt;addCreative($creative);
      }
  }
  </code></pre>
  
  <p>related methodds:</p>
  
  <pre><code> /**
   * @param Creative $creative
   */
  public function removeCreative(Creative $creative)
  {
      if($this-&gt;creatives-&gt;contains($creative)) {
          $creative-&gt;setCampaign(null);
          $this-&gt;creatives-&gt;removeElement($creative);
      }
  }
  
  /**
   * @param Creative $creative
   */
  public function addCreative(Creative $creative)
  {
      if(!$this-&gt;creatives-&gt;contains($creative)) {
          $this-&gt;creatives-&gt;add($creative);
          $creative-&gt;setCampaign($this);
      }
  }
  </code></pre>
  
  <p>So now I'm updating Campaign and Creative objects but still, it's not detaching creatives from campaign</p>
  
  <p><strong>Updated</strong>. I found this article <a href="https://stackoverflow.com/questions/24145326/doctrine-how-to-unset-set-null-onetomany-relationship">Doctrine: How to unset (SET NULL) OneToMany relationship</a> but I do not see what the difference between my code and solution? I should nnot use persist? But in my logic I update alot of Campaign fields and  I need to persist campaign before flush</p>