php 如何在 Doctrine ORM 中使用 createQueryBuilder 选择特定列?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12543650/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:46:37 来源:igfitidea点击:
How can I select specific Columns with createQueryBuilder in Doctrine ORM?
提问by Carlos Saez Alfonsea
I'm using Doctrine createQueryBuilder()to construct queries in Symfony2. But, I don't want to take all columns in this entity. How can I select only the ID and Name?
我正在使用 DoctrinecreateQueryBuilder()在 Symfony2 中构建查询。但是,我不想获取此实体中的所有列。如何仅选择 ID 和名称?
$query = $this->getEntityManager()->createQueryBuilder();
$query
->select('d')
->from('AcmeBundle:Demo', 'd')
->leftjoin('d.otherEntity', 'o');
$query->setMaxResults(10);
$results = $query->getQuery()->getResult();
Thank you so much,
非常感谢,
回答by Mun Mun Das
Try following,
尝试以下,
$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();

