你好我有一个有两个主键的实体。
class UsefulnessEvaluation
{
/**
* @ORM\Id
* @ORM\ManyToOne(targetEntity="App\EvaluationBundle\Entity\Evaluation", cascade={"persist","remove"})
*/
private $evaluation;
/**
* @ORM\Id
* @ORM\ManyToOne(targetEntity="App\UserBundle\Entity\User", cascade={"persist","remove"})
*/
private $user;
/**
*
* @ORM\Column(type="string", nullable=false)
*/
private $type;
/**
* @ORM\Column(type="datetime", nullable=false)
*/
private $createdAt;
//etc
}
我想在存储库中计算评估的数量:
class UsefulnessEvaluationRepository extends EntityRepository
{
public function countEvaluationLikes($evaluation_id)
{
$query = $this->getEntityManager()->createQuery(
'SELECT count(p.evaluation) as nbre
FROM AppEvaluationBundle:UsefulnessEvaluation p
WHERE p.evaluation = :id
)->setParameter('id', $evaluation_id);
return $query->getSingleScalarResult();
}
}
这是错误:
将具有复合主键的实体绑定到查询不是 支持的。您应该将参数拆分为显式字段和 单独绑定它们。
答案 0 :(得分:1)
我认为问题在于您选择count(p.evaluation)
,但由于您已经指定了p.evaluation的ID,因此您似乎没有必要保证获得非空p.evaluation的值。
试试这个
$query = $this->getEntityManager()->createQuery(
'SELECT count(p) as nbre
FROM AppEvaluationBundle:UsefulnessEvaluation p
WHERE IDENTITY(p.evaluation) = :id'
)->setParameter('id', $evaluation_id);