Question

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 !

Was it helpful?

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'

OTHER TIPS

$qb->where accepts a string (DQL), or an Query Builder expression.

In your case, replace the line by:

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

I woud also rewrite the beginning like this:

$qb = $this->_em->createQueryBuilder()
    ->select($qb->expr()->count('abonnement'));
    ->from('TeeltFicheBundle:Abonnement', 'abonnement')
;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top