title: Doctrine ORM: use interface as relation for different entities?
body: <p>How to use interface in many-to-many relation in doctrine?</p>
  
  <p>In my app there is 3 entities: User, Car and Driver. User can add cars and drivers as favorites. So i made this structure (simplified):</p>
  
  <p>User, who has favorite feature:</p>
  
  <pre><code>namespace Acme\AppBundle\Entities;
  
  use Acme\AppBundle\Interfaces\HasFavorites;
  
  /** @ORM\Entity */
  class User implements HasFavorites
  {
      /** @ORM\ManyToMany(targetEntity="Acme\AppBundle\Entities\Favorite") */
      protected $favorites;
  
      public function getFavorites() : ArrayCollection
      {
          return $this-&gt;favorites;
      }
  
      public function addFavorite(Favorite $favorite)
      {
          $this-&gt;favorites-&gt;add($favorite);
      }
  }
  </code></pre>
  
  <p>Favorite object model:</p>
  
  <pre><code>namespace Acme\AppBundle\Entities;
  
  use Acme\AppBundle\Interfaces\Favoritable;
  
  /** @ORM\Entity */
  class Favorite
  {
      /** @ORM\ManyToOne(targetEntity="Acme\AppBundle\Entities\User") */
      private $owner;
  
      /** @ORM\ManyToOne(targetEntity="Acme\AppBundle\Interfaces\Favoritable") */
      private $target;
  
      public function __construct(User $owner, Favoritable $target)
      {
          $this-&gt;owner  = $owner;
          $this-&gt;target = $target;
      }
  
      public function getOwner() : User
      {
          return $this-&gt;owner;
      }
  
      public function getTarget() : Favoritable
      {
          return $this-&gt;target;
      }
  }
  </code></pre>
  
  <p>Car and Driver – entities that can be added to favorites:</p>
  
  <pre><code>namespace Acme\AppBundle\Entities;
  
  use Acme\AppBundle\Interfaces\Favoritable;
  
  /** @ORM\Entity */
  class Car implements Favoritable { /* ... */ }
  
  /** @ORM\Entity */
  class Driver implements Favoritable { /* ... */ }
  </code></pre>
  
  <p>But when i update my schema with command <code>./bin/console doctrine:schema:update --force</code>, i'll get error</p>
  
  <pre><code>[Doctrine\Common\Persistence\Mapping\MappingException]
  Class 'Acme\AppBundle\Interfaces\Favoritable' does not exist
  </code></pre>
  
  <p>This code in my tests also working ok (if i don't work with database) so namespaces and file paths are correct:</p>
  
  <pre><code>$user = $this-&gt;getMockUser();
  $car  = $this-&gt;getMockCar();
  $fav  = new Favorite($user, $car);
  $user-&gt;addFavorite($fav);
  static::assertCount(1, $user-&gt;getFavorites());
  static::assertEquals($user, $fav-&gt;getUser());
  </code></pre>
  
  <p>How to do this relation? What i've found yet in search that is only situation when Car/Driver are mostly the same by logic.</p>
  
  <p>What i need in database is just something like this (desired) but it's not so important:</p>
  
  <pre><code>+ ––––––––––––– +   + ––––––––––––– +   + –––––––––––––––––––––––––––––––––– +
  |     users     |   |      cars     |   |            favorites               |
  + –– + –––––––– +   + –– + –––––––– +   + –––––––– + ––––––––– + ––––––––––– +
  | id |   name   |   | id |   name   |   | owner_id | target_id | target_type |
  + –– + –––––––– +   + –– + –––––––– +   + –––––––– + ––––––––– + ––––––––––– +
  | 42 | John Doe |   | 17 | BMW      |   |       42 |        17 | car         |
  + –– + –––––––– +   + –– + –––––––– +   + –––––––– + ––––––––– + ––––––––––– +
  </code></pre>