title: Symfony Association Mapping OneToOne and OneToMany to Same Entity
body: <p>I have a <code>View</code> entity that represents the primary page record, and then I have an associated entity called <code>ViewVersion</code> which stores multiple versions of the entity as it's changed over time. The <code>View</code> entity sets the current "Published" <code>ViewVersion</code> in the <code>VersionId</code> field. This makes for a simple OneToOne association. But in some contexts I will also want to get all the versions associated with this <code>View</code> entity, e.g. if I want to allow the user to review older versions and revert back. So I will need another mapping which is a OneToMany. The first <code>viewVersion</code> will map to the active "published" version, and the second <code>viewVersions</code> will show all the versions.</p>
  
  <p><strong>Entity Definitions</strong></p>
  
  <pre><code>/**
  * @ORM\Entity
  * @ORM\Table(name="view")
  * @ORM\Entity(repositoryClass="Gutensite\CmsBundle\Entity\View\ViewRepository")
  */
  class View extends Entity\Base {
  
      /**
      * @ORM\OneToOne(targetEntity="\Gutensite\CmsBundle\Entity\View\ViewVersion", inversedBy="view", cascade={"persist", "remove"}, orphanRemoval=true)
      * @ORM\JoinColumn(name="versionId", referencedColumnName="id")
      */
      protected $viewVersion;
  
      /**
       * @ORM\Column(type="integer", nullable=true)
       */
      protected $versionId = NULL;
  
      /**
   * @ORM\OneToMany(targetEntity="\Gutensite\CmsBundle\Entity\View\ViewVersion", mappedBy="viewAll", cascade={"persist", "remove"}, orphanRemoval=true)
   */
  protected $viewVersions;
  }
  
  /**
  * @ORM\Entity
  * @ORM\Table(name="view_version")
  * @ORM\Entity(repositoryClass="Gutensite\CmsBundle\Entity\View\ViewVersionRepository")
  */
  class ViewVersion extends Entity\Base {
  
      /**
      * @ORM\OneToOne(targetEntity="\Gutensite\CmsBundle\Entity\View\View", mappedBy="viewVersion", cascade={"persist"})
      */
      protected $view;
  
      /**
      * @ORM\ManyToOne(targetEntity="\Gutensite\CmsBundle\Entity\View\View", inversedBy="viewVersions")
      * @ORM\JoinColumn(name="viewId", referencedColumnName="id")
      */
      protected $viewAll;
  
      /**
      * The primary view entity that this version belongs to.
      * @ORM\Column(type="integer", nullable=true)
      */
      protected $viewId;
  
  }
  </code></pre>
  
  <p>This "works" but is it recommended to have two associations with the same entity like this? Or is this a really bad idea? </p>
  
  <p>The <code>ViewVersion</code> entity will reference a single <code>View</code> entity in both cases, but the mapped associations need two separate variables, e.g. <code>View</code> and <code>ViewAll</code>. I'm not exactly sure how the internals work for the association, and how the reference variable with the mapping is used.</p>
  
  <p>Alternatively, I could get rid of the OneToOne association, and just set a ViewRepository function to get the current published version based on the versionId (just like the old mapped entity used to do with the getVersion()). That would work, but is it more internal overhead, because it would make two queries... or will Doctrine be smart enough to optimize this, just like it did with the getVersion().</p>
  
  <p><strong>NOTE:</strong>
  These <a href="https://stackoverflow.com/questions/7647584/hibernate-mapping-onetomany-and-onetoone-on-child-object-property">other</a> <a href="https://stackoverflow.com/questions/8545229/doctrine2-using-the-same-entity-in-both-onetomany-and-onetoone-possible-solve">answers</a> are not complete.</p>
  
  <p><strong>References:</strong>
  <a href="http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/working-with-associations.html" rel="nofollow noreferrer">http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/working-with-associations.html</a>
  <a href="http://doctrine-orm.readthedocs.org/en/2.0.x/reference/association-mapping.html#one-to-many-bidirectional" rel="nofollow noreferrer">http://doctrine-orm.readthedocs.org/en/2.0.x/reference/association-mapping.html#one-to-many-bidirectional</a></p>