title: Doctrine 2: inserting data into an entity with associations
body: <p>Solved by adding a conditional when adding a new entity to the database, it checks if the entity is not null... apparently there was null entities trying to get saved. Now the controller code looks like this:</p>
  
  <pre><code>...
  $ciudades_id = explode(';', $this-&gt;getRequest()-&gt;getParam('ciudades_id'));
             foreach($ciudades_id as $ciudad_id){
                 $ciudad = $this-&gt;_em-&gt;find("Application_Model_Ciudades", intval($ciudad_id));
                 if($ciudad!= null){
                  $carrera-&gt;getCiudad()-&gt;add($ciudad);                  
                 }
             }
  
  
             $instituciones_id = explode(';', $this-&gt;getRequest()-&gt;getParam('instituciones_id'));
             foreach($instituciones_id as $institucion_id){
                 $institucion = $this-&gt;_em-&gt;find("Application_Model_Instituciones", intval($institucion_id));
                 if($institucion != null){
                    $carrera-&gt;getInstituciones()-&gt;add($institucion); 
                 }
             }
  ...
  </code></pre>
  
  <p><strong>Thanks to the guys that helped at #doctrine IRC channel :)</strong></p>
  
  <hr>
  
  <p>This is my problem... I got an entity called "Carreras" (carreers) that has some associations, and the new carreers are added to the database with a web form. A carreer for me is a test, which has questions and other atttributes, and the user can select the cities and institutions that apply for that test.</p>
  
  <p>So i'm getting this error when i try to save new data on the entity:</p>
  
  <pre><code>An error occurred
  Application error
  Exception information:
  
  Message: A new entity was found through the relationship
  'Application_Model_Carreras#ciudad' that was not configured 
  to cascade persist operations for entity: Doctrine\ORM\UnitOfWork@. 
  Explicitly persist    the new entity or configure cascading persist 
  operations on the relationship. If you cannot find out which entity 
  causes the problem implement 'Application_Model_Ciudades#__toString()' 
  to get a clue.
  </code></pre>
  
  <p>This is the model for "Carreras"</p>
  
  <pre><code>use Doctrine\Common\Collections\ArrayCollection;
  /**
   * @Entity
   * @Table(name="carreras")
   */
  class Application_Model_Carreras
  {
      /**
       * @Id @Column(type="integer")
       * @GeneratedValue
       */
      private $id;
  
      /** @Column(type="string") */
      private $nombre;
  
      /**
       * @ManyToMany(targetEntity="Application_Model_PruebasCarrera")
       * @JoinTable(name="Carreras_PruebasCarrera",
       *      joinColumns={@JoinColumn(name="carreras_id", referencedColumnName="id")},
       *      inverseJoinColumns={@JoinColumn(name="pruebascarrera_id", referencedColumnName="id")}
       *      )
       */
      private $pruebas;
  
  
      /** @Column(type="integer") */
      private $valor;
  
      /** @Column(type="date") */
      private $fechainicio;
  
      /** @Column(type="date") */
      private $fechafin;
  
      /**
       * This association causes error
       * @ManyToMany(targetEntity="Application_Model_Ciudades")
       * @JoinTable(name="carrera_ciudades",
       *      joinColumns={@JoinColumn(name="carrera_id", referencedColumnName="id")},
       *      inverseJoinColumns={@JoinColumn(name="ciudad_id", referencedColumnName="id")}
       *      )
       */
      private $ciudad;
  
      /**
       * @ManyToMany(targetEntity="Application_Model_Instituciones")
       * @JoinTable(name="carrera_instituciones",
       *      joinColumns={@JoinColumn(name="carrera_id", referencedColumnName="id")},
       *      inverseJoinColumns={@JoinColumn(name="institucion_id", referencedColumnName="id")}
       *      )
       */
      private $instituciones;
  
      public function __construct()
      {
          $this-&gt;pruebas = new ArrayCollection();
          $this-&gt;ciudad = new ArrayCollection();
          $this-&gt;instituciones = new ArrayCollection();
      }
  
      public function setNombre($nombre){
          $this-&gt;nombre = $nombre;
      }
  
      public function setValor($valor){
          $this-&gt;valor = $valor;
      }
  
      public function setFechainicio($fechainicio){
          $this-&gt;fechainicio = $fechainicio;
      }
  
      public function setFechafin($fechafin){
          $this-&gt;fechafin = $fechafin;
      }
  
      public function getCiudad(){
          return $this-&gt;ciudad;
      }
  
      public function getPruebas(){
          return $this-&gt;pruebas;
      }
  
      public function getInstituciones(){
          return $this-&gt;instituciones;
      }
  }
  </code></pre>
  
  <p>Now the action at controller:</p>
  
  <pre><code> /*
       * This is an action for adding a new career
       */
      public function agregarAction()
      {
         $formtest = new Admin_Form_AgregarCarrera();
         $this-&gt;view-&gt;formtest = $formtest;
         if ($this-&gt;getRequest()-&gt;isPost() &amp;&amp; $this-&gt;view-&gt;formtest-&gt;isValid($this-&gt;_getAllParams()))
         {
             /*
              * If the form is okay creating new Carreer model object
              * This model has some attributes and three associations (for now) 
              * you can see them later in detail
              */
             $carrera = new Application_Model_Carreras();
             $carrera-&gt;setNombre($this-&gt;getRequest()-&gt;getParam("nombre"));
             $carrera-&gt;setValor($this-&gt;getRequest()-&gt;getParam("valor"));
             $carrera-&gt;setFechainicio(new \DateTime($this-&gt;getRequest()-&gt;getParam("fechainicio")));
             $carrera-&gt;setFechafin(new \DateTime($this-&gt;getRequest()-&gt;getParam("fechafin")));
             /*
              * This is the first association. It's working fine (for now)
              */
             $pruebas = $this-&gt;getRequest()-&gt;getParam("pruebas");
             foreach($pruebas as $prueba){
                if($prueba != '0'){
                    $tmp = $this-&gt;_em-&gt;find("Application_Model_PruebasCarrera", $prueba);
                    $carrera-&gt;getPruebas()-&gt;add($tmp);
                }
             }
             /*
              * This is the second associations, i'm getting the error with this one
              */
             $ciudades_id = explode(';', $this-&gt;getRequest()-&gt;getParam('ciudades_id'));
             foreach($ciudades_id as $ciudad_id){
                 $ciudad = $this-&gt;_em-&gt;find("Application_Model_Ciudades", intval($ciudad_id));
                 $carrera-&gt;getCiudad()-&gt;add($ciudad);
             }
             /*
              * This is the third one. Nothing to say about this.
              */
             $instituciones_id = explode(';', $this-&gt;getRequest()-&gt;getParam('instituciones_id'));
             foreach($instituciones_id as $institucion_id){
                 $institucion = $this-&gt;_em-&gt;find("Application_Model_Instituciones", intval($institucion_id));
                 $carrera-&gt;getInstituciones()-&gt;add($institucion);
             }
  
             $this-&gt;_em-&gt;persist($carrera);
             $this-&gt;_em-&gt;flush();
             //$this-&gt;redirector-&gt;gotoSimpleAndExit('index','Carrera','admin');     
          }
      }
  </code></pre>
  
  <p>Well i don't know what else to show... please comment if you can help me :)</p>
  
  <p>--edit </p>
  
  <p>I added cascade={"persist"} in the associations of the model "Carreras" and the error changed:</p>
  
  <pre><code>An error occurred
  Application error
  Exception information:
  
  Message: Class Doctrine\ORM\UnitOfWork is not a 
  valid entity or mapped super class. 
  </code></pre>
  
  <p>Now this is "Ciudades" model, it stores the cities available for the test and is associated with the institutions that exist on every city.</p>
  
  <pre><code>use Doctrine\Common\Collections\ArrayCollection;
  /**
   * @Entity
   * @Table(name="ciudades")
   */
  class Application_Model_Ciudades {
      /**
       * @Id @Column(type="integer")
       * @GeneratedValue
       */
      private $id;
  
      /** @Column(type="string") */
      private $ciudad;
  
      /** @Column(type="string") */
      private $departamento;
  
      /** @Column(type="string") */
      private $pais;
  
      /**
       * @ManyToMany(targetEntity="Application_Model_Instituciones")
       * @JoinTable(name="Ciudades_Instituciones",
       *      joinColumns={@JoinColumn(name="ciudades_id", referencedColumnName="id")},
       *      inverseJoinColumns={@JoinColumn(name="instituciones_id", referencedColumnName="id")}
       *      )
       */
      private $instituciones;
  
      public function __construct()
      {
          $this-&gt;instituciones = new ArrayCollection();
      }
  
      public function getCiudad(){
          return $this-&gt;ciudad;
      }
  
      public function getId(){
          return $this-&gt;id;
      }
  
      public function getInstituciones(){
          return $this-&gt;instituciones;
      }
  
  }
  </code></pre>
  
  <p>Now this is "Instituciones" Model, it stores the institutions available for the tests.</p>
  
  <pre><code>/**
   * @Entity
   * @Table(name="instituciones")
   */
  class Application_Model_Instituciones {
      /**
       * @Id @Column(type="integer")
       * @GeneratedValue
       */
      private $id;
  
      /** @Column(type="string") */
      private $nombre;
  
      public function getId(){
          return $this-&gt;id;
      }
  
      public function getNombre(){
          return $this-&gt;nombre;
      }
  
  }
  </code></pre>
  
  <p>Now this is "PruebasCarrera" Model, for me this model entity stores the questions of the tests, and every question can have a partner who supports the question:</p>
  
  <pre><code>use Doctrine\Common\Collections\ArrayCollection;
  /**
   * @Entity
   * @Table(name="pruebas_carrera")
   */
  class Application_Model_PruebasCarrera extends Application_Model_PruebasBase{
  
      /**
       * @Id @Column(type="integer")
       * @GeneratedValue
       */
      private $id;
  
      /**
       * @ManyToMany(targetEntity="Application_Model_Patrocinadores")
       * @JoinTable(name="pruebascarrera_patrocinadores",
       *      joinColumns={@JoinColumn(name="pruebas_id", referencedColumnName="id", unique="true")},
       *      inverseJoinColumns={@JoinColumn(name="patrocinadores_id", referencedColumnName="id", unique=false)}
       *      )
       */
      protected $patrocinadores;
  
      /** @Column(type="string") */
      private $respuesta; 
  
      public function __construct() {
          $this-&gt;patrocinadores = new ArrayCollection();
      }
  
      public function setRespuesta($respuesta){
          $this-&gt;respuesta = $respuesta;
      }
  
      public function getPatrocinadores(){
          return $this-&gt;patrocinadores;
      }
  
      public function getId(){
          return $this-&gt;id;
      }
  
      public function getRespuesta(){
          return $this-&gt;respuesta;
      }
  
  }
  </code></pre>