Search code examples
inheritancesymfonydoctrine-ormclass-table-inheritance

Doctrine2 Inheritance and querybuilder


I'm using Inheritance with Doctrine 2.1 :

Fiche is the master entity and Artist is derived from Fiche

so : Fiche -> Artist

then I have this method, in another repository called Abonnement :

public function getCountAbonnes(\MyApp\FicheBundle\Entity\Fiche $fiche){

    $qb = $this->_em->createQueryBuilder();

    $qb->add('select', $qb->expr()->count('abonnement'));
    $qb->from('\Teelt\FicheBundle\Entity\Abonnement', 'abonnement');

    // !!!! this line is the problem !!!!
    $qb->where('fiche', $fiche);

    return $qb->getQuery()->getSingleScalarResult();
}

Here's the Abonnement ORM definition :

MyApp\FicheBundle\Entity\Abonnement:
    type: entity
    repositoryClass: MyApp\FicheBundle\Repository\AbonnementRepository
    table: abonnement

    # many fiche for many users
    manyToOne:
      user:
        targetEntity: MyApp\UserBundle\Entity\User
        inversedBy: abonnements
      fiche:
        targetEntity: MyApp\FicheBundle\Entity\Fiche
        inversedBy: abonnements

# etc ...

The problem I have here is that I always pass the Artist entity instead of the Fiche, and I get this error :

Expression of type 'Teelt\FicheBundle\Entity\Artist' not allowed in this context.

So I guess I must fetch the Fiche from my Artist ... which sounds bad since it's the same object !


Solution

  • Thanks for your answer, it got me closer to the solution,

    but at the end it seems that :

    $qb->where($qb->expr()->eq('abonnement.fiche', $fiche->getId() ));
    

    was the solution.

    Is it normal ? I thought that the mapping of the ID would be automatic, but if I don't use the getId() It returns a toString() version of my Fiche and generate an :

    SELECT COUNT(abonnement) FROM MyAppFicheBundle:Abonnement abonnement WHERE abonnement.fiche = Miles Davis

    [Syntax Error] line 0, col 100: Error: Expected end of string, got 'Davis'