title: Best way to update entity one to many relationship in Symfony 2 / Doctrine 2
body: <p>What is the best way to update an entities one to many relationship in doctine?
  For example: I have an entity called booking with a mapped one to many Guest entities.</p>
  
  <p>When a booking is edited, the number of guests can change by either adding or removing guests.
  At the moment, I count the number of guests submitted, and if they are different to the current number of guests, i delete the booking guests and re add the new ones!</p>
  
  <p>To me this doesn't seem right and would like to know how other people approach this.</p>
  
  <p>Code example:</p>
  
  <pre><code>    if (count($collection)) {
          $numberGuests = count($this-&gt;getEntity()-&gt;getGuests());
          foreach ($collection as $guest) {
              if ($numberGuests != count($guests)) {
                  // delete guest if the number has changed
                  $this-&gt;getGuestManager()-&gt;delete($guest);
              } else {
                  // update entity
                  $guest-&gt;setArrayData(Tools::getData($i, $guests));
              }
          }
      }
  </code></pre>