Symfony2 Doctrine查询实体错误

时间:2015-11-04 09:38:21

标签: php mysql entity-framework symfony doctrine-orm

我会在查询中使用一个字段,但是我收到此错误: [语义错误]第0行,第78行附近' resource_id =':错误:类Dt \ TagBundle \ Entity \ Tagging没有名为resource_id的字段或关联

这是我的实体标记:

<?php

 namespace Dt\TagBundle\Entity;
 use Doctrine\ORM\Mapping as ORM;
 use Doctrine\ORM\Mapping\UniqueConstraint;
 use FPN\TagBundle\Entity\Tagging as BaseTagging;


 /**
  * Dt\TagBundle\Entity\Tagging
  *
  * @ORM\Table(uniqueConstraints={@UniqueConstraint(name="tagging_idx",      columns={"tag_id", "resource_type", "resource_id"})})
  * @ORM\Entity
  */
 class Tagging extends BaseTagging
 {
/**
 * @var integer $id
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
protected $id;

/**
 * @ORM\ManyToOne(targetEntity="Dt\TagBundle\Entity\Tag")
 * @ORM\JoinColumn(name="tag_id", referencedColumnName="id")
 **/
protected $tag;
 }

我的数据库上的表是:标记(id,tag_id,resource_type,resource_id) 其中 tag_id 对应于表的id:Tag(id,name) 和 resource_id 对应用户表User(id,name,surname,...)

这是我的搜索查询:

$query = $em->createQueryBuilder()
         ->select('uu')   
         ->from('DtEcBundle:User', 'uu')
         ->innerJoin("DtTagBundle:Tag","tg", "WITH", "tg.id = tig.tag")
         ->innerJoin("DtTagBundle:Tagging","tig", "WITH", "tig.resource_id = uu.id")
         ->innerJoin("DtTagBundle:Tag","tg", "WITH", "tg.id = tig.tag") 
         ->where("uu.surname LIKE :search OR uu.name LIKE :search OR uu.profession LIKE :search OR tg.name LIKE :search")
         ->setParameter("search",$search.'%')
            ->getQuery();

如何定义resource_id?感谢

1 个答案:

答案 0 :(得分:0)

如果查看实体基类的源代码,您会发现该字段名为resourceId而不是resource_id。它保存在数据库的resource_id列中,但这不是您需要在Doctrine查询中使用的列。所以,你应该把它改成:

     ->innerJoin("DtTagBundle:Tagging","tig", "WITH", "tig.resourceId = uu.id")