你好,我有一个有两个主键的实体。
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();
}
}
这是错误:
Binding an entity with a composite primary key to a query is not supported. You should split the parameter into the explicit fields and bind them separately.
最佳答案
我认为问题在于您正在选择 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);
关于symfony - 学说查询错误 : Binding an entity with a composite primary key to a query,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24022505/