Inheritance Doctrine2继承和querybuilder

Inheritance Doctrine2继承和querybuilder,inheritance,symfony,doctrine-orm,class-table-inheritance,Inheritance,Symfony,Doctrine Orm,Class Table Inheritance,我使用的是继承原则2.1: Fiche是主实体 而艺术家是从菲希衍生出来的 所以:菲希->艺术家 然后我在另一个名为Abonnement的存储库中使用了这个方法: public function getCountAbonnes(\MyApp\FicheBundle\Entity\Fiche $fiche){ $qb = $this->_em->createQueryBuilder(); $qb->add('select', $qb->expr()-&g

我使用的是继承原则2.1:

Fiche是主实体 而艺术家是从菲希衍生出来的

所以:菲希->艺术家

然后我在另一个名为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();
}
以下是Abonnement ORM的定义:

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 ...
我在这里遇到的问题是,我总是传递艺术家实体,而不是Fiche,我得到以下错误:

在此上下文中不允许使用类型为“Teelt\FicheBundle\Entity\Artist”的表达式


所以我想我必须从我的艺术家那里得到菲希。。。这听起来很糟糕,因为它是同一个物体

其中接受字符串(DQL)或查询生成器表达式

在您的情况下,将该行替换为:

$qb->where($qb->expr()->eq('abonnement.fiche', $fiche));
我也会这样重写开头:

$qb = $this->_em->createQueryBuilder()
    ->select($qb->expr()->count('abonnement'));
    ->from('TeeltFicheBundle:Abonnement', 'abonnement')
;

谢谢你的回答,这让我更接近解决方案

但最终看来:

$qb->where($qb->expr()->eq('abonnement.fiche', $fiche->getId() ));
这就是解决办法

这正常吗?我认为ID的映射是自动的,但如果我不使用getId(),它将返回我的Fiche的toString()版本,并生成:

从MyAppFicheBundle中选择计数(abonnement):abonnement abonnement WHERE abonnement.fiche=迈尔斯戴维斯

[语法错误]第0行,第100列:错误:应为字符串结尾,得到“Davis”

可能是这样的:
(Fiche)$Fiche
,我知道它不起作用,但在这个想法中。。。