title: Doctrine 2 : Custom repositories and inheritance
body: <p>Hello Stack Overflowers,</p>
  
  <p>Working with Doctrine 2, I encounter some troubles regarding custom repositories and inheritance.</p>
  
  <p>Long story short, I want to make that kind of structure :</p>
  
  <ul>
  <li>BaseEntityRepository : contains generic methods such as findByXXX() based on called class name</li>
  <li>SomeEntityRepository : contains specific methods related to the entity type</li>
  </ul>
  
  <p>The code for those classes looks like this :</p>
  
  <p>BaseEntityRepository :</p>
  
  <pre><code>namespace model\repositories;
  
  use \Doctrine\ORM\EntityRepository;
  
  class BaseEntityRepository extends EntityRepository {
      public function findByID($id) {
          $result = null;
  
          try {
              $dql = "SELECT a FROM " . get_called_class() . " a WHERE a.id = :id";
              $query = $this-&gt;_em-&gt;createQuery($dql);
              $query-&gt;setParameter("id", $id);
              $result = $query-&gt;getSingleResult();
          } catch (\Exception $ex) {
              echo $ex-&gt;getMessage();
          }
  
          return $result;
      }
  }
  </code></pre>
  
  <p>SomeEntityRepository :</p>
  
  <pre><code>namespace model\repositories;
  
  class SomeEntityRepository extends BaseEntityRepository {
  
  }
  </code></pre>
  
  <p>My test sample code :</p>
  
  <pre><code>$repo = $em-&gt;getRepository("model\\entities\\SomeEntity");
  $result = $repo-&gt;findByID($id);
  </code></pre>
  
  <p>With this code, I expect <code>$repo</code> to have access to the <code>findByID($id)</code> method by inheritance from <code>BaseEntityRepository</code>. Of course, the <code>repositoryClass</code> annotation for <code>SomeEntity</code> targets <code>SomeEntityRepository</code>. On the contrary, <code>BaseEntityRepository</code> is NOT targeted as a standalone repository class.</p>
  
  <p>The thing is, all I get is an exception :</p>
  
  <blockquote>
    <p>Class "model\repositories\SomeEntityRepository" sub class of
    "model\repositories\BaseEntityRepository" is not a valid entity or
    mapped super class.</p>
  </blockquote>
  
  <p>I can't, for the sake of me, figure out what's wrong in this code. Maybe I missed something but my research towards inheritance in repositories did not bring any satisfying results. I guess there is some kind of restriction on repositories such as for them to work properly, all should be targeted as repositoryClass at least once or something like that, but that doesn't please me.</p>
  
  <p>If you have any kind of insight, piece of advice or bits of solution, I'm all ears! Thanks again.</p>