我正在使用createQueryBuilder在Symfony2中构建查询。但是,我不想在这个实体中占用所有列。如何只选择ID和名称?
$query = $this->getEntityManager()->createQueryBuilder();
$query
->select('d')
->from('AcmeBundle:Demo', 'd')
->leftjoin('d.otherEntity', 'o');
$query->setMaxResults(10);
$results = $query->getQuery()->getResult();
非常感谢你,
答案 0 :(得分:54)
尝试以下,
$fields = array('d.id', 'd.name', 'o.id');
//$fields = 'partial d.{id, name}, partial o.{id}'; //if you want to get entity object
$query = $this->getEntityManager()->createQueryBuilder();
$query
->select($fields)
->from('AcmeBundle:Demo', 'd')
->leftjoin('d.otherEntity', 'o');
$query->setMaxResults(10);
$results = $query->getQuery()->getResult();