title: Persist an entity and relate it to another entity without persiting the second again
body: <p>I am building a front-end app with Durandal/Knockoutjs and a web service back-end with Symfony2 and I use Doctrine to access the database. </p>
  
  <p>I have two entities that are in association of one-to-many, like this</p>
  
  <pre><code>use Doctrine\ORM\Mapping as ORM;
  
  /**
   *  @ORM\Entity
   *  @ORM\Table(name = "drew_cat_door")
   */
  class Door
  {
      public function __construct()
      {
          //$this-&gt;setAddTime(new \DateTime(date('Y-m-d H:i:s')));
  
          if ($this-&gt;getAddTime() == null)
              $this-&gt;setAddTime(new \DateTime(date('Y-m-d H:i:s')));
          else
              $this-&gt;setUpdateTime(new \DateTime(date('Y-m-d H:i:s')));
      }
  
      /**
       *  @ORM\Id
       *  @ORM\Column(type = "integer")
       *  @ORM\GeneratedValue(strategy = "AUTO")
       */
      protected $id;
  
      /**
       *  @ORM\Column(type = "string", length = 30)
       */
      protected $name;
  
      /**
       *  @ORM\ManyToOne(targetEntity = "DoorType", inversedBy = "doors")
       *  @ORM\JoinColumn(name = "type_id", referencedColumnName = "id")
       */
      protected $type;
  
      /**
       *  @ORM\Column(type = "string", length = 30, nullable = true)
       */
      protected $filename;
  
      /**
       *  @ORM\Column(type = "string", length = 100, nullable = true)
       */
      protected $description;
  
      /**
       *  @ORM\Column(type = "integer", nullable = true)
       */
      protected $views;
  
      /**
       *  @ORM\Column(type = "datetime")
       */
      protected $add_time;
  
      /**
       *  @ORM\Column(type = "datetime", nullable = true)
       */
      protected $update_time;
  
      /**
       * Get id
       *
       * @return integer 
       */
      public function getId()
      {
          return $this-&gt;id;
      }
  
      /**
       * Set name
       *
       * @param string $name
       * @return Door
       */
      public function setName($name)
      {
          $this-&gt;name = $name;
  
          return $this;
      }
  
      /**
       * Get name
       *
       * @return string 
       */
      public function getName()
      {
          return $this-&gt;name;
      }
  
      /**
       * Set filename
       *
       * @param string $filename
       * @return Door
       */
      public function setFilename($filename)
      {
          $this-&gt;filename = $filename;
  
          return $this;
      }
  
      /**
       * Get filename
       *
       * @return string 
       */
      public function getFilename()
      {
          return $this-&gt;filename;
      }
  
      /**
       * Set description
       *
       * @param string $description
       * @return Door
       */
      public function setDescription($description)
      {
          $this-&gt;description = $description;
  
          return $this;
      }
  
      /**
       * Get description
       *
       * @return string 
       */
      public function getDescription()
      {
          return $this-&gt;description;
      }
  
      /**
       * Set views
       *
       * @param integer $views
       * @return Door
       */
      public function setViews($views)
      {
          $this-&gt;views = $views;
  
          return $this;
      }
  
      /**
       * Get views
       *
       * @return integer 
       */
      public function getViews()
      {
          return $this-&gt;views;
      }
  
      /**
       * Set add_time
       *
       * @param \DateTime $addTime
       * @return Door
       */
      public function setAddTime($addTime)
      {
          $this-&gt;add_time = $addTime;
  
          return $this;
      }
  
      /**
       * Get add_time
       *
       * @return \DateTime 
       */
      public function getAddTime()
      {
          return $this-&gt;add_time;
      }
  
      /**
       * Set update_time
       *
       * @param \DateTime $updateTime
       * @return Door
       */
      public function setUpdateTime($updateTime)
      {
          $this-&gt;update_time = $updateTime;
  
          return $this;
      }
  
      /**
       * Get update_time
       *
       * @return \DateTime 
       */
      public function getUpdateTime()
      {
          return $this-&gt;update_time;
      }
  
      /**
       * Set type
       *
       * @param \Drewkol\AdminBundle\Entity\DoorType $type
       * @return Door
       */
      public function setType(\Drewkol\AdminBundle\Entity\DoorType $type = null)
      {
          $this-&gt;type = $type;
  
          return $this;
      }
  
      /**
       * Get type
       *
       * @return \Drewkol\AdminBundle\Entity\DoorType 
       */
      public function getType()
      {
          return $this-&gt;type;
      }
  }
  
  /**
   *  @ORM\Entity
   *  @ORM\Table(name = "drew_cat_doortype")
   */
  class DoorType
  {
      public function __construct()
      {
          $this-&gt;doors = new ArrayCollection();
  
          if ($this-&gt;getAddTime() == null)
              $this-&gt;setAddTime(new \DateTime(date('Y-m-d H:i:s')));
          else
              $this-&gt;setUpdateTime(new \DateTime(date('Y-m-d H:i:s')));
      }
      /**
       *  @ORM\Id
       *  @ORM\Column(type = "integer")
       *  @ORM\GeneratedValue
       */
      protected $id;
  
      /**
       *  @ORM\Column(type = "string", length = 30)
       */
      protected $name;
  
      /**
       *  @ORM\OneToMany(targetEntity = "Door", mappedBy = "type")
       */
      protected $doors;
  
      /**
       *  @ORM\Column(type = "datetime")
       */
      protected $add_time;
  
      /**
       *  @ORM\Column(type = "datetime", nullable = true)
       */
      protected $update_time;
  
      /**
       * Get id
       *
       * @return integer 
       */
      public function getId()
      {
          return $this-&gt;id;
      }
  
      /**
       * Set name
       *
       * @param string $name
       * @return DoorType
       */
      public function setName($name)
      {
          $this-&gt;name = $name;
  
          return $this;
      }
  
      /**
       * Get name
       *
       * @return string 
       */
      public function getName()
      {
          return $this-&gt;name;
      }
  
      /**
       * Set add_time
       *
       * @param \DateTime $addTime
       * @return DoorType
       */
      public function setAddTime($addTime)
      {
          if ($addTime != null)
              $this-&gt;add_time = $addTime;
  
          return $this;
      }
  
      /**
       * Get add_time
       *
       * @return \DateTime 
       */
      public function getAddTime()
      {
          return $this-&gt;add_time;
      }
  
      /**
       * Set update_time
       *
       * @param \DateTime $updateTime
       * @return DoorType
       */
      public function setUpdateTime($updateTime)
      {
          $this-&gt;update_time = $updateTime;
  
          return $this;
      }
  
      /**
       * Get update_time
       *
       * @return \DateTime 
       */
      public function getUpdateTime()
      {
          return $this-&gt;update_time;
      }
  
      /**
       * Add doors
       *
       * @param \Drewkol\AdminBundle\Entity\Door $doors
       * @return DoorType
       */
      public function addDoor(\Drewkol\AdminBundle\Entity\Door $doors)
      {
          $this-&gt;doors[] = $doors;
  
          return $this;
      }
  
      /**
       * Remove doors
       *
       * @param \Drewkol\AdminBundle\Entity\Door $doors
       */
      public function removeDoor(\Drewkol\AdminBundle\Entity\Door $doors)
      {
          $this-&gt;doors-&gt;removeElement($doors);
      }
  
      /**
       * Get doors
       *
       * @return \Doctrine\Common\Collections\Collection 
       */
      public function getDoors()
      {
          return $this-&gt;doors;
      }
  }
  </code></pre>
  
  <p>Sorry for not ommiting any code. As you can see, a Door has a DoorType.</p>
  
  <p>As it is easy, when adding a door, I post a JSON that goes </p>
  
  <pre><code>{"name":"nowe","type":{"id":5,"name":"loluk","add_time":"2013-09-25T01:05:05+0200"},"description":"hehe\n","filename":"hehe.jpg"}
  </code></pre>
  
  <p>and is a full door entity model with a type that has already been around. When I try to add this entity with doctrine with this code</p>
  
  <pre><code>$json_door = $this-&gt;get("request")-&gt;getContent();
  if (empty($json_door))
      return $this-&gt;createBadRequestException();
  
  $door = $this-&gt;container-&gt;get("serializer")
      -&gt;deserialize($json_door, "Drewkol\\AdminBundle\\Entity\\Door", "json");
  $door-&gt;setAddTime(new \DateTime(date('Y-m-d H:i:s')));
  
  $manager = $this-&gt;getDoctrine()-&gt;getManager();
  $manager-&gt;persist($door);
  $manager-&gt;flush();
  </code></pre>
  
  <p>I get an error that says </p>
  
  <pre><code>A new entity was found through the relationship 'Drewkol\AdminBundle\Entity\Door#type' that was not configured to cascade persist operations for entity: Drewkol\AdminBundle\Entity\DoorType@000000002d1b74500000000063b1c8fb. To solve this issue: Either explicitly call EntityManager#persist() on this unknown entity or configure cascade persist this association in the mapping for example @ManyToOne(..,cascade={"persist"}). If you cannot find out which entity causes the problem implement 'Drewkol\AdminBundle\Entity\DoorType#__toString()' to get a clue. 
  </code></pre>
  
  <p>My question is: <i>what is the most clean and efficient way to add door with a type that has already been added? </i></p>
  
  <p>Is there any way to tell Doctrine to try and resolve if the type given in the entity being added is already in the database (as it <i>must have been added before</i>) or do I have to fetch and remove the type from the JSON deserialized entity to prevent it from being persisted (with the suggested cascade option) and then fetch the type with Doctrine and then set it as a type of my brand new deserialized door entity so Doctrine is aware of the type? I mean I like the data model generated by knockoutjs and transfered with JSON and it seems to me to be a general drawback not being able to do it the way I presented. </p>