title: Symfony2 - Share Entity Between Bundles with different relationships
body: <p>How do you share an entity between multiple bundles with different relationships?</p>
  
  <p>For example both the ZooAnimalBundle and FarmAnimalBundle need a User Entity. A third Bundle AccountUserBundle has the User Entity. </p>
  
  <p>In both the Zoo and Farm AnimalBundles I create a User Entity like so:</p>
  
  <pre><code>use Account\UserBundle\Entity\User as BaseUser;
  
  
  class User extends BaseUser 
  {
  }
  </code></pre>
  
  <p>I then have a Hospital entity in Zoo:</p>
  
  <pre><code>class Hospital {
  /**
   * @ORM\ManyToMany(targetEntity="Zoo\AnaimalBundle\Entity\User")
   * @ORM\JoinTable(name="users_zoo_animals")
   */
  protected $users; 
  </code></pre>
  
  <p>And a Room entity in Farm:</p>
  
  <pre><code>class Room {
  /**
   * @ORM\ManyToMany(targetEntity="Farm\AnaimalBundle\Entity\User")
   * @ORM\JoinTable(name="users_farm_animals")
   */
  protected $users; 
  </code></pre>
  
  <p>Everything works so far in that I can call Zoo.Room->getUsers() or Farm.Hospital->getUsers()</p>
  
  <p>However the problem is I'm not sure on how to set up the inverse relationship in their respective User entities.</p>
  
  <p>If for example I update the FarmAnimal User Entity and run doctrine:generate:entities</p>
  
  <pre><code>/**
   * @ORM\Entity
   */
  class User extends BaseUser
  {
      /**
       * @ORM\ManyToMany(targetEntity="Room", mappedBy="users", cascade={"persist"})
       */
      protected $rooms;
  }
  </code></pre>
  
  <p>It will copy the protected $properties from BaseUser and create all the set and get methods which is not what I want. What is the correct way of setting up these relationships?</p>
  
  <p><strong>Update</strong></p>
  
  <p>If you don't setup the inverse relationship, how would you select all users where hospital.id = 1 </p>
  
  <pre><code>    $qb = $this-&gt;getEntityManager()-&gt;createQueryBuilder()
          -&gt;select(
              'u'
          )
          -&gt;from('Account\UserBundle\Entity\User','u')
          -&gt;leftJoin('u.hospitals', 'h')
          -&gt;andWhere('h.id = :hospital_id')
              -&gt;setParameter('hospital_id',$hospital_id);
  </code></pre>
  
  <p>This gives the error:</p>
  
  <pre><code>   Class Account\UserBundle\Entity\User has no association named hospitals 
  </code></pre>
  
  <p>I know I could select from hospital and join user because that relationship does exist but I need to select users because I am using them with Doctrine\ORM\Tools\Pagination\Paginator</p>
  
  <p>The query would be</p>
  
  <pre><code>    $qb = $this-&gt;createQueryBuilder('a')
          -&gt;select(
              'h', 'u'
          )
          -&gt;leftJoin('h.users', 'u')
  </code></pre>
  
  <p>The problem with this is Paginator only sees one result Hospital because the Users are attached to it.</p>