找不到命名查询

时间:2013-09-03 14:54:10

标签: symfony doctrine-orm

在Symfony控制器中,我有以下功能:

protected function getDisplayTags(Expertise $expertise, Person $user){
    $rep = $this->getDoctrine()->getRepository('ExpertiseDefaultBundle:Tag');

    $q = $rep->createNamedQuery('fetchTagsByExpertise'); //PROBLEM HERE

    $results = $q->setParameters(array('person'=>$user->getId()), array('expertise'=>$expertise->getId()))->getResult();
    return $results;
}

抛出此错误:No query found named 'fetchTagsByExpertise' on class 'Expertise\DefaultBundle\Entity\Tag'.存储库是Doctrine提供的通用存储库。

我通过标记实体中的注释定义了named native query fetchTagsByExpertise

namespace Expertise\DefaultBundle\Entity;

use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\ORM\Mapping as ORM;


/**
 * @ORM\Entity
 * @ORM\Table(name="tag")
 * @ORM\NamedNativeQueries({
 *      @ORM\NamedNativeQuery(
 *          name            = "fetchTagsByExpertise",
 *          resultSetMapping= "mappingTagsByExpertise",
 *          query   ="SELECT t.id AS t_id, t.tag AS t_tag,
                    a.expertise_id AS a_expertise_id,
                        COUNT(a.person_id) AS tagcount,
                        SUM(CASE
                            WHEN a.person_id = :person THEN 1
                            ELSE 0
                            END) AS has_curuser
                        FROM expertise_tags a JOIN tag t ON (a.tag_id = t.id)
                        WHERE a.expertise_id = :expertise
                        GROUP BY t.tag , a.expertise_id
                        ORDER BY t.tag"
 *      ),
 * })
 * @ORM\SqlResultSetMappings({
 *      @ORM\SqlResultSetMapping(
 *          name    = "mappingTagsByExpertise",
 *          entities= {
 *              @ORM\EntityResult(
 *                  entityClass = "__CLASS__",
 *                  fields      = {
 *                      @ORM\FieldResult(name = "id",      column="t_id"),
 *                      @ORM\FieldResult(name = "tag",     column="t_tag"),
 *                  }
 *              ),
 *              @ORM\EntityResult(
 *                  entityClass = "AssociatedTag",
 *                  fields      = {
 *                      @ORM\FieldResult(name = "expertise_id", column="a_expertise_id"),
 *                  }
 *              )
 *          },
 *          columns = {
 *              @ORM\ColumnResult("tagcount"),
 *              @ORM\ColumnResult("has_cursuser")
 *          }
 *      )
 *})
 */
class Tag
{ //vanilla Doctrine entity. NO foreign keys
  // ...
}

已加入的entityClass AssociatedTag具有由(tag_id, expertise_id, person_id)组成的复合主键。我已经尝试在查询和映射中包含其余的键,但它没有效果。'

我在定义命名本机查询的方式和位置上有什么问题吗?为什么存储库找不到它?

1 个答案:

答案 0 :(得分:3)

正确的方法是调用createNativeNamedQuery而不是createNamedQuery。

protected function getDisplayTags(Expertise $expertise, Person $user){
    $rep = $this->getDoctrine()->getRepository('ExpertiseDefaultBundle:Tag');

    $q = $rep->createNativeNamedQuery('fetchTagsByExpertise'); // NO PROBLEM HERE ;)

    $results = $q->setParameters(array('person'=>$user->getId()), array('expertise'=>$expertise->getId()))->getResult();

    return $results;
}