symfony - 学说查询错误 : Binding an entity with a composite primary key to a query

标签 symfony doctrine-orm repository

你好,我有一个有两个主键的实体。

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/

相关文章:

symfony - 使用 Symfony2 包含第 3 方文件的最佳方法

php - Symfony2 Doctrine 遍历 while next()

svn - Subversion 究竟如何在存储库中存储文件?

git - Git:将两个存储库彼此 merge

asp.net-mvc - ASP.NET MVC/EF4/POCO/Repository - 如何更新关系?

postgresql - 无效的文本表示 : 7 ERROR: invalid input syntax for uuid: "test"

mysql - 网站中的搜索功能?

mysql - DQL 无法在 FROM 子句中指定目标表 'user_addresses' 进行更新

php - 重复多对多关系

php - Doctrine ManyToMany 同一实体重复输入错误