title: What's the best way to load a sub object from an id posted from Zend Framework 2 Form
body: <p>I have 2 classes, Widget &amp; Person. The Widget class references an instance of Person as it's owner. They are stored/retrieved from the database via doctrine2.</p>
  
  <p>I have created a Zend Framework 2 Form to enter the details of a Widget which includes a select where you can choose the owner of the Widget, this Form element posts back the id of the Person object to be set at the owner.</p>
  
  <p>In it's current state the setOwner() method of Widget class will take an object of type Person or an id which can then be loaded from the database using the Doctrine2 entity manager.</p>
  
  <p>While this works I think it is not the best way as the Widget object is dealing with the loading from the database and it needs to have access to the entity manager to do this.</p>
  
  <p>As I currently understand the only 2 ways I can do this is either the way I have it working or by creating a new Hydrator which will fish the object out of the database before Hydrating the object or is there another method?</p>
  
  <p>I want the architecture to be as tidy as possible so I would like to know what people think would be the best way to do this in ZF2?</p>
  
  <p>This is the setup I currently have:</p>
  
  <pre><code>class Widget {
      protected $id;
      protected $name;
      protected $owner;
  
      // Doctrine Entity Manager
      protected $entityManager;
  
     public function __construct($entityManager)
     {
         $this-&gt;entityManager = $entityManager;
     }
  
      // methods, getters &amp; setters
  
      public function setOwner($owner)
      {
          if (is_object($owner) &amp;&amp; $owner instanceof Person)
          {
              $this-&gt;owner = $owner;
          }
          else if (is_scalar($owner)
          {
              $this-&gt;owner = $this-&gt;entityManager-&gt;find('Person', $owner);
          }
      }
  }
  
  class Person {
      protected $id;
      protected $name;
  
      // other member variables &amp; methods
  }
  </code></pre>