我试图根据另一表中的信息从一个表中获取信息,该表由ID链接。
两个表是: property
和 unit
。
我需要收集属性中的所有单元,但只有当属性的状态为" 1"和隐藏的标志为" 0"时。在正常的mysql中,我写道:
SELECT u.* FROM unit u INNER JOIN property p ON p.id = u.property WHERE p.status = 1 AND p.hidden = 0
会产生正确的结果,尽管当我使用QueryBuilder尝试相同时:
$qb = $this->getEntityManager()->createQueryBuilder();
$qb->select('u')
->from('AppBundle:Unit', 'u')
->join('u', 'AppBundle:Property', 'p', 'u.property = p.id')
->where('p.status = :status')
->andWhere('p.hidden = :hidden')
->setParameter('status', 1)
->setParameter('hidden', 0);
return $qb->getQuery()->getResult();
使用我从学说查询构建器文档中收集的信息。但是,当我加载页面时,我会收到以下错误:
[语义错误]第0行,col 42附近'u appbundle:属性':错误: 'u'类未定义。
执行的查询:
SELECT u FROM AppBundle:Unit u INNER JOIN u AppBundle:Property P u.property = p.id WHERE p.status = :status AND p.hidden = :hidden
谁能帮助找出我在查询中做错了什么?
尝试更改以下内容:
->join('u', 'AppBundle:Property', 'p', 'u.property = p.id')
:
->join('AppBundle:Property', 'p', 'WITH', 'u.property = p.id')
您应该交换第一个和第二个参数位置,因为join()方法是:
/**
* Creates and adds a join over an entity association to the query.
*
* The entities in the joined association will be fetched as part of the query
* result if the alias used for the joined association is placed in the select
* expressions.
*
* <code>
* $qb = $em->createQueryBuilder()
* ->select('u')
* ->from('User', 'u')
* ->join('u.Phonenumbers', 'p', ExprJoin::WITH, 'p.is_primary = 1');
* </code>
*
* @param string $join The relationship to join.
* @param string $alias The alias of the join.
* @param string|null $conditionType The condition type constant. Either ON or WITH.
* @param string|null $condition The condition for the join.
* @param string|null $indexBy The index for the join.
*
* @return QueryBuilder This QueryBuilder instance.
*/
public function join($join, $alias, $conditionType = null, $condition = null, $indexBy = null)
这是学说QueryBuilder类中的文档。