title: How do you save a record with a OneToMany relationship field with Doctrine 2?
body: <p>how do you save a record (from a service) that has one of it's properties set as a OneToMany relationship with another entity?</p>
  
  <p>I have the following service class for my users:</p>
  
  <pre><code>namespace Federico\Entity;
  use Federico\Entity\User;
  /**
   * Description of UserService
   *
   * @author fiodorovich
   */
  class UserService {
  protected $em;
  
  public function __construct ($em) {
      $this-&gt;em = $em;
  }
  
  public function saveUser($user) {
      if ($user['id'] != null) { //update 
          $entity = $this-&gt;getUser($user['id']);
  
          //do something
  
          if (!$entity)
              throw new Exception('Error saving user!');
      } else { //insert 
          $entity = new User();
  
          foreach ($user as $k =&gt; $v) {
              if ($k !== 'submit') {
                  if ($k == 'password') {
                      //echo $this-&gt;_salt;die;
                      $entity-&gt;$k = $this-&gt;createPass($v);
                  } else {
                      $entity-&gt;$k = $v;
                  }
              }
          }
      }
      $this-&gt;em-&gt;persist($entity);
      $this-&gt;em-&gt;flush(); //save the user
  }
  
  //do something else...
  }
  </code></pre>
  
  <p>But when I try saving the user and the countries property (a collection which refers to a OneToMany relationship oneUser-manyCountries) is set I get a "Class does not exist" exception.</p>
  
  <p><strong>EDIT:</strong> User and Countries entities</p>
  
  <pre><code>namespace Federico\Entity;
  use Doctrine\Common\Collections\ArrayCollection;
  /**
   * Description of User
   * @Table(name="users")
   * @Entity
   * @author fiodorovich
   */
  class User 
  {
  /**
   * @var integer
   * @Id @Column (name="id", type="integer", nullable=false)
   * @GeneratedValue(strategy="AUTO")
   * 
   */
  private $id;
  
  /**
   * @Column(type="string",length=60,nullable=true, unique=true)
   * @var string
   */
  private $email;
  
  /**
   * @Column(type="string",length=60,nullable=true)
   * @var string
   */
  private $password;
  
  /**
   * @Column(type="string",length=60,nullable=true,unique=true)
   * @var string
   */
  private $url;
  
  /**
   * @Column(type="string",length=60,nullable=true,unique=true)
   * @var string
   */
  private $responsable;
  
  /**
   * @Column(type="string",length=20,nullable=true)
   * @var string
   */
  private $role;
  
  /**
   *
   * @var datetime
   * @Column(type="datetime", nullable=false)
   */
  private $created;
  
  /**
   * 
   * @param \Doctring\Common\Collections\Collection $property
   * @OneToMany(targetEntity="Countries",mappedBy="user", cascade={"persist", "remove"})
   */
  private $countries;
  
  public function __construct()
  {
      $this-&gt;created = new \DateTime(date('Y-m-d H:i:s'));
      $this-&gt;countries = new ArrayCollection();
  
  }
  
  public function __get($property) 
  {
      return $this-&gt;$property;
  }
  
  public function __set($property, $value)
  {
      $this-&gt;$property = $value;
  }
  
  public function getCountries()
  {
      return $this-&gt;countries;
  }
  }
  </code></pre>
  
  <p>And Countries entity:</p>
  
  <pre><code>namespace Federico\Entity;
  /**
   * @Table(name="countries")
   * @Entity
   * @author fiodorovich
   * 
   */
  class Countries {
  /**
   * @var integer
   * @Id @Column (name="id", type="integer", nullable=false)
   * @GeneratedValue(strategy="AUTO")
   * 
   */
  private $id;
  
  /**
   *
   * @var string
   * @Column(type="string") 
   */
  private $countryName;
  
  /**
   *
   * @var User
   * @ManyToOne(targetEntity="User", inversedBy="id") 
   * @JoinColumns({
   *  @JoinColumn(name="user_id", referencedColumnName="id")
   * })
   */
  private $user;
  
  
  public function __get($property) 
  {
      return $this-&gt;$property;
  }
  
  public function __set($property, $value)
  {
      $this-&gt;$property = $value;
  }
  
  }
  </code></pre>