php 如何在 Doctrine 2 中获取类而不是数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10482085/
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
How to fetch class instead of array in Doctrine 2
提问by flower58
I am able to fetch my data from database by using this structure:
我可以使用以下结构从数据库中获取数据:
$user = $this->getDoctrine()
->getRepository('AcmeDemoBundle:Emails')
->find(8081);
When I do that, I am able to get my data like this:
当我这样做时,我能够像这样获取我的数据:
$user->getColumnNameHere();
Basically I am able to use EntityClass.
基本上我可以使用实体类。
But if I want to use QueryBuilder instead of findI am only getting associative arrays.
但是如果我想使用 QueryBuilder 而不是find我只得到关联数组。
$product->createQueryBuilder('p')
->setMaxResults(1)
->where('p.idx = :idx')
->select('p.columnNameHere')
->setParameter('idx', 8081)
->orderBy('p.idx', 'DESC')
->getQuery();
$product = $query->getResult();
$product returnds as array. Is it possible to fetch it withj Entity Managaer Class? If yes, how?
$product 作为数组返回。是否可以使用j Entity Managaer Class来获取它?如果是,如何?
I digg the documentation but it seems not possible or not exist in the doc or I'm just blind :)
我挖掘了文档,但文档中似乎不可能或不存在,或者我只是瞎了:)
回答by Florian Klein
Yes you can, usually using:
是的,你可以,通常使用:
$repository
->createQueryBuilder('p')
->getQuery()
->execute()
;
This should return you an array of entities.
这应该返回一个实体数组。
If you want to get a single entity result, use either getSingleResultor getOneOrNullResult:
如果您想获得单个实体结果,请使用getSingleResult或getOneOrNullResult:
$repository
->createQueryBuilder('p')
->getQuery()
->getOneOrNullResult()
;
Warning: These method can potentially throw NonUniqueResultException.
警告:这些方法可能会抛出NonUniqueResultException.
Edit:Ok, so the question was about partial objects: http://docs.doctrine-project.org/en/latest/reference/partial-objects.html
编辑:好的,所以问题是关于部分对象:http: //docs.doctrine-project.org/en/latest/reference/partial-objects.html
回答by ismaail E.G.
you can get an object instead of array by using "Partial Objects".
您可以使用“部分对象”来获取对象而不是数组。
here is a tested example with DoctrineORM 2.2.2:
这是 DoctrineORM 2.2.2 的测试示例:
// create query builder
// $em is the EntityManager
$qb = $em->createQueryBuilder();
// specify the fields to fetch (unselected fields will have a null value)
$qb->select ('partial p.{id,pubDate,title,summary}')
->from ('Project\Entity\Post', 'p')
->where ('p.isActive = 1')
->orderBy ('p.pubDate', 'desc');
$q = $qb->getQuery();
$result = $q->getResult();
var_dump($result); // => object
回答by HappyCoder
If you wish to return an object from your original query:
如果您希望从原始查询中返回一个对象:
$product->createQueryBuilder('p')
->setMaxResults(1)
->where('p.idx = :idx')
->select('p.columnNameHere')
->setParameter('idx', 8081)
->orderBy('p.idx', 'DESC')
->getQuery();
$product = $query->getResult();
Remove this line
删除此行
->select('p.columnNameHere')
As soon as you use select, it will return an array...
只要您使用 select,它就会返回一个数组...
回答by Crozin
getResult()method returns a collection (an array) of entities. Use getSingleResult()if you're going to fetch only one object.
getResult()方法返回实体的集合(数组)。使用getSingleResult(),如果你要获取只有一个对象。
EDIT:
编辑:
Oh, I just noticed that you want to fetch a single field of a single object. Use getSingleScalarResult()as @Florian suggests.
哦,我刚刚注意到您想要获取单个对象的单个字段。getSingleScalarResult()按照@Florian 的建议使用。

