1

在 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 提供的通用存储库。

我通过标记实体中的注释定义了命名的本机查询: 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
  // ...
}

连接的 entityClassAssociatedTag有一个复合主键,由(tag_id, expertise_id, person_id). 我已经尝试在查询和映射中包含它的其余键,但它没有效果。

我定义命名本机查询的方式或位置是否有问题?为什么仓库找不到呢?

4

1 回答 1

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;
}
于 2014-12-11T03:44:54.457 回答