不能在doctrine2中使用JOIN与相关实体

时间:2013-12-05 17:01:57

标签: php sql symfony doctrine-orm

我是guided by this,我无法理解出了什么问题。

我的实体:

/**
 * @ORM\Entity
 * @ORM\Table(name="term")
 */
class Term {
    /**
     * @ORM\Column(type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;
    /**
     * @ORM\OneToMany(targetEntity="Description", mappedBy="term")
     **/ 
    private $description;

    //....
}

/**
 * @ORM\Entity
 * @ORM\Table(name="description")
 */
class Description {

    /**
     * @ORM\Column(type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;
    /**
     * @Orm\ManyToOne(targetEntity="term", inversedBy="description")
     * @Orm\JoinColumn(name="term_id", referencedColumnName="id")
     **/  
    private $term;
    /**
     * @ORM\Column(type="string", length=8)
     */
    private $normativity;
    //...
}

我需要通过其中一个字段(示例中的规范性)获取术语和过滤术语描述。

我试过了:

$query = $this->getDoctrine()->getEntityManager()
    ->createQuery("
        SELECT term, desc FROM myTerminologyBundle:Term term
        JOIN term.description desc
        WHERE term.word LIKE :r_word' and desc.normativity IN :norm"         
    )->setParameter('r_word', '%'.$word.'%')->setParameter('norm', array());

我得到以下例外情况:

  

[语法错误]第0行,第30列:错误:预期的IdentificationVariable   | ScalarExpression | AggregateExpression |功能声明|   PartialObjectExpression | “(”Subselect“)”| CaseExpression,得到了   '降序'

3 个答案:

答案 0 :(得分:1)

尝试这样的事情:

createQuery("
    SELECT term
    FROM myTerminologyBundle:Term term
    INNER JOIN myTerminologyBundle:Description desc
    WHERE term.word LIKE :r_word' and desc.normativity IN :norm"         
)

答案 1 :(得分:0)

使用dql

$query= $this->getEntityManager()
                        ->createQueryBuilder()
                        ->select('term.id as id,term.name as name')
                        ->from('FROM myTerminologyBundle:Term ', 'term')
                        ->innerJoin('term.Description ', 'desc')
                        ->Where('term.word LIKE=:term')
                        ->setParameters(array('term' => $term))
                         ->orWhere('desc.normativity  IN(:category_id)')
                        ->setParameter('category_id',$category_id)

                        ->getQuery();
             return $query->getResult();

答案 2 :(得分:0)

SQL中有保留字 desc 。我将它改为另一个并且有效。