【问题标题】:Error in doctrine query : Binding an entity with a composite primary key to a query学说查询中的错误:将具有复合主键的实体绑定到查询
【发布时间】:2014-07-24 05:19:43
【问题描述】:

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

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();
  }

}

这是错误:

将具有复合主键的实体绑定到查询不是 支持的。您应该将参数拆分为显式字段和 分别绑定。

【问题讨论】:

    标签: symfony doctrine-orm repository


    【解决方案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);
    

    【讨论】:

      猜你喜欢
      • 2016-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-20
      • 1970-01-01
      • 2018-10-25
      • 1970-01-01
      相关资源
      最近更新 更多