我被这件事所引导,我不明白出了什么问题。
我的实体:
/**
* @ORMEntity
* @ORMTable(name="term")
*/
class Term {
/**
* @ORMColumn(type="integer")
* @ORMId
* @ORMGeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORMOneToMany(targetEntity="Description", mappedBy="term")
**/
private $description;
//....
}
/**
* @ORMEntity
* @ORMTable(name="description")
*/
class Description {
/**
* @ORMColumn(type="integer")
* @ORMId
* @ORMGeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @OrmManyToOne(targetEntity="term", inversedBy="description")
* @OrmJoinColumn(name="term_id", referencedColumnName="id")
**/
private $term;
/**
* @ORMColumn(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 | FunctionDeclaration|PartialObjectExpression |"("Subselect")"|CaseExpression,得到"desc"
试试这样的东西:
createQuery("
SELECT term
FROM myTerminologyBundle:Term term
INNER JOIN myTerminologyBundle:Description desc
WHERE term.word LIKE :r_word' and desc.normativity IN :norm"
)
使用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();
SQL中有保留字desc。我把它换成另一个就行了。