我正在尝试个性化symfony 2.4
存储库查询以仅检索某些字段。平面字段一切正常,但在检索实体字段时,我只获取id(默认情况下),但不获取整个实体数据。我的问题:
$select = $this->createQueryBuilder('ca')
->select('ca.id, ca.name')
->leftJoin('ca.users', 'user')
->addSelect('(user) as users');
$select->setMaxResults($count);
return $select->getQuery()->getResult();
结果是:[{id: 1, name: "Some name", users: 1}, ...]
如何更改此查询以便用户包含整个用户数据,例如ID,名称,地址等?
答案 0 :(得分:2)
你应该这样试试。您可以使用Partial。
$select = $this->createQueryBuilder('ca')
->select('partial ca.{id, name}')
->leftJoin('ca.users', 'users')
->addSelect('users');
此处提供了此问题的详细说明。 Doctrine2: Cannot select entity through identification variables without choosing at least one root entity alias
答案 1 :(得分:1)
改变这个:
->select('ca.id, ca.name')
到此:
->select('ca')
答案 2 :(得分:1)
这对我有用:
$select = $this->createQueryBuilder('ca')
->select('partial ca.{id, name}, users')
->leftJoin('ca.users', 'users');
$select->setMaxResults($count);
return $select->getQuery()->getArrayResult();
答案 3 :(得分:1)
我希望这对你有用。
$qb = $this->em->createQueryBuilder();
$qb->select('o.id as order_id,o.createdBy as created_by,o.user as user_id');
// for getting selective fields
//$qb->select('o');
//for getting all fields in certain table
$qb->from('Entity\Orders', 'o');
$qb->setMaxResults(1);
// for getting single record only.
return $qb->getQuery()->getOneOrNullResult();
// for getting single record or null record.
//return $qb->getQuery()->getResult();
// for getting multiple records.
随时询问有关它的任何疑问。