title: How do you populate an Entity with foreign keys from an Array in Symfony2/doctrine
body: <p>Consider an entity which has a foreign key.  We receive an array which contains the foreign key as an integer and we want to populate the current entity from the array (this could be either an update or a create, makes no difference).  So far I have been unable in the documentation to find an example of how to perform this update in the "symfony2/doctrine" way.  </p>
  
  <p>If we change the type of the private variable to be an integer instead of an object of the type of the Foreign entity, we lose the ORM mapping/auto-instantiation/etc.  If we leave as is, we cannot set it with a simple integer.  </p>
  
  <p>Doctrine documentation dictates we shall not access the entity manager from within an Entity (for the purpose of "find"ing the foreign entity from the key before setting current entities value) and anyway I haven't found documentation of how to do it if I wanted to.  Best practices dictate that population of an object from an array should occur as an object method on that object.  Common sense dictates that a simple array should be supported and should not require end user/controller to know to create the foreign entity.  </p>
  
  <p>Can someone point me in the direction of sanity? </p>
  
  <p>Example Code:</p>
  
  <pre><code>&lt;?php
  namespace Prefix\MyBundle\Entity;
  use Doctrine\ORM\Mapping as ORM;
  class Item
  {
      private $itemid;
  
      /*
               * --- omitted for brevity ---
               */
      private $categoryid;
      public function getItemid()
      {
          return $this-&gt;itemid;
      }
      /**
       * --- omitted for brevity again ---
       */
      public function setCategoryid(\Prefix\MyBundle\Entity\Category $categoryid = null)
      {
          $this-&gt;categoryid = $categoryid;
  
          return $this;
      }
      public function getCategoryid()
      {
          return $this-&gt;categoryid;
      }
      public function fromArray($data = array())
      {
          $updated = false;
          if ( isset($data['Category']) )
          {
              /* We know that $data['Category'] will be an int */
              $this-&gt;setCategoryid($data['Category']); //Fails invalid type!
              $updated = true;
          }
          return $updated;
      }
  }
  </code></pre>