但是,为了完整起见,您可以对查询生成器执行相同的操作,如下所示:
$queryBuilder = $this->getEntityManager()->createQueryBuilder();
$queryBuilder
->select('user')
->from('UserBundle:User', 'user', 'user.id')
->where('user.id = :userId')
->setParameter('userId', $userId)
;
var_dump(
$queryBuilder->getQuery()->getArrayResult()
);
如您所见,索引选项可用作查询构建器from
方法的第三个参数:
/**
* Creates and adds a query root corresponding to the entity identified by the given alias,
* forming a cartesian product with any existing query roots.
*
* <code>
* $qb = $em->createQueryBuilder()
* ->select('u')
* ->from('User', 'u')
* </code>
*
* @param string $from The class name.
* @param string $alias The alias of the class.
* @param string $indexBy The index for the from.
*
* @return QueryBuilder This QueryBuilder instance.
*/
public function from($from, $alias, $indexBy = null)
{
return $this->add('from', new Expr\From($from, $alias, $indexBy), true);
}