title: Symfony2: inserting the one without the many of a one-to-many
body: <p>I continue to struggle with the Symfony/Doctrine approach to creating objects.  The present issue is how best to create an object on the one side of a one-to-many without creating an object on the many side.  The problem seems to stem from the many side being the owning side of the relationship and thus the determinant in whether an object is persisted.</p>
  
  <p>Particulars: a client entity has a one-to-many relationship with member entity.  A client can be created by itself with </p>
  
  <pre><code>        $client  = new Client();
          $form = $this-&gt;createForm(new ClientType(), $client);
          $form-&gt;bind($request);
          if ($form-&gt;isValid()) {
              $em = $this-&gt;getDoctrine()-&gt;getManager();
              $em-&gt;persist($client);
              $em-&gt;flush();
              return $this-&gt;redirect($this-&gt;generateUrl('client_show', array('id' =&gt; $client-&gt;getId())));
          }
  </code></pre>
  
  <p>and a client plus member or members, but not by itself with </p>
  
  <pre><code>$client  = new Client();
  $member = new Member();
  $client-&gt;addMember($member);
  $form = $this-&gt;createForm(new ClientType(), $client);
  $form-&gt;bind($request);
  if ($form-&gt;isValid()) {
      $em = $this-&gt;getDoctrine()-&gt;getManager();
      $member-&gt;setClient($client);
      $em-&gt;persist($member);
      $em-&gt;flush();
      return $this-&gt;redirect($this-&gt;generateUrl('client_show', array('id' =&gt; $client-&gt;getId())));
  </code></pre>
  
  <p>Client Entity:</p>
  
  <pre><code>class Client
  {
      /**
       * @var integer $id
       *
       * @ORM\Column(name="id", type="integer", nullable=false)
       * @ORM\Id
       * @ORM\GeneratedValue(strategy="IDENTITY")
       */
      private $id;
  
      /**
      * @ORM\OneToMany(targetEntity="Member", mappedBy="client")
      * 
      */
      protected $members;
  
      public function __construct()
      {
          $this-&gt;members = new ArrayCollection();
      }
  
      public function getMembers()
      {
          return $this-&gt;members;
      }
  
      public function setMembers($members)
      {
          foreach ($members as $members)
          {
              $members-&gt;addClient($this);
          }
          $this-&gt;members = $members;
          return $this;
      }
  
      public function addMember(Member $member)
      {
          $this-&gt;members-&gt;add($member);
  //        $member-&gt;setClient($this);
          return $this;
  //        $this-&gt;members[] = $member;
      }
  
      public function removeMember(Member $member)
      {
          $this-&gt;members-&gt;removeElement($member);
          return $this;
      } 
  </code></pre>
  
  <p>Member Entity:</p>
  
  <pre><code>class Member 
  {
  
      /**
       * @var integer $id
       *
       * @ORM\Column(name="hid", type="integer", nullable=false)
       * @ORM\Id
       * @ORM\GeneratedValue(strategy="IDENTITY")
       */
      private $id;
  
       /**
       * @ORM\ManyToOne(targetEntity="Client",inversedBy="members",cascade={"remove", "persist"})
       * @ORM\JoinColumn(name="clientId", referencedColumnName="id")
       * 
       */
       protected $client;
  
      public function setClient(Client $client)
      {
          $this-&gt;client = $client;
          return $this;
      }
  
      public function getClient()
      {
          return $this-&gt;client;
      }
  </code></pre>
  
  <p>The above code throws an error if there is no member inserted.  Can a single bit of code work to add a client with or without a member be specified?</p>