title: Initializing empty relationships in entities
body: <p>I have entities with 1:1 or 1:M relations to other entities. All relations however are nullable. </p>
  
  <p>I want to proxy some operations to the related entity. I'm giving example below. The problem is that if the relation still does not exist, I have <code>null</code>, so I'm ending up constantly checking for nulls, which obviously is wrong. What I would like to do is to hydrate my entities with empty objects. Reasons:</p>
  
  <ul>
  <li>Doctrine  knows what instance should be created for the field anyway. So it should just provide empty instance instead of null</li>
  <li>I don't want to fill my code with initializations, like
  <code>$object-&gt;setSettings(new SettingsEntity)</code></li>
  <li>If the requests should be proxied is somehow disputable, but I want to hide the DB representation from the client code. If my direction however is totally wrong, please point me to the right direction. I may accept that this is responsibility of the model, not of the entity, but Doctrine always returns entities to me</li>
  </ul>
  
  <p>Sure, I can add the initialization either in the constructor of the entity, or to provide getter that creates a new instance of the object, if such does not exists. There are couple of reasons I don't want this:</p>
  
  <ul>
  <li>I don't know how objects are actually hydrated. I assume such initialization should happen in an event and not in the constructor</li>
  <li>I don't want to write the code for each entity (at some point, someone will forget to add the initialization in the getter) and want to make it automatically for each relation instead.</li>
  </ul>
  
  <p>Some example code: </p>
  
  <pre><code>/**
   * SomeObject
   * @ORM\Entity()
   * @ORM\Table(
       name="some_object"
   * )
  */ class SomeObject implements DataTransfer {
   /**
     * @ORM\OneToOne(targetEntity="Settings", mappedBy="SomeObject")
     */
    protected $settings;
  
    public function getSettings() {
      return $this-&gt;settings;
    }
  
    public function get() {
      $record = new \stdClass();
      $record-&gt;id = $this-&gt;getId();
      ...
  
      $settingsObject = $this-&gt;getSettings();
      $record-&gt;someKey = $settingsObject ? $settingsObject-&gt;getSomeKey() : null;
      $record-&gt;someOtherKey = $settingsObject ? $settingsObject-&gt;getSomeOtherKey() : null;
      return $record;
    }
  </code></pre>
  
  <p>Any suggestions, including hacking Doctrine, are welcome.</p>
  
  <p>P.S. Doctrine-ORM version is 2.3. I can upgrade if this will help solving the problem.</p>