How to fetch class instead of array in Doctrine 2(如何在 Doctrine 2 中获取类而不是数组)
问题描述
我可以使用这种结构从数据库中获取我的数据:
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();
基本上我可以使用 Entity 类.
Basically I am able to use Entity Class.
但是如果我想使用 QueryBuilder 而不是 find
我只会得到关联数组.
But if I want to use QueryBuilder instead of find
I am only getting associative arrays.
$product->createQueryBuilder('p')
->setMaxResults(1)
->where('p.idx = :idx')
->select('p.columnNameHere')
->setParameter('idx', 8081)
->orderBy('p.idx', 'DESC')
->getQuery();
$product = $query->getResult();
$product 作为数组返回.是否可以使用j Entity Managaer Class来获取它?如果是,如何?
$product returnds as array. Is it possible to fetch it withj Entity Managaer Class? If yes, how?
我挖掘了文档,但文档中似乎不可能或不存在,或者我只是瞎了:)
I digg the documentation but it seems not possible or not exist in the doc or I'm just blind :)
推荐答案
可以,通常使用:
$repository
->createQueryBuilder('p')
->getQuery()
->execute()
;
这应该会返回一个实体数组.
This should return you an array of entities.
如果您想获得单个实体结果,请使用 getSingleResult
或 getOneOrNullResult
:
If you want to get a single entity result, use either getSingleResult
or getOneOrNullResult
:
$repository
->createQueryBuilder('p')
->getQuery()
->getOneOrNullResult()
;
警告:这些方法可能会抛出NonUniqueResultException
.
Warning: These method can potentially throw NonUniqueResultException
.
好的,所以问题是关于部分对象的:http://docs.doctrine-project.org/en/latest/reference/partial-objects.html
Ok, so the question was about partial objects: http://docs.doctrine-project.org/en/latest/reference/partial-objects.html
这篇关于如何在 Doctrine 2 中获取类而不是数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 Doctrine 2 中获取类而不是数组


- 如何从数据库中获取数据以在 laravel 中查看页面? 2022-01-01
- PHP - if 语句中的倒序 2021-01-01
- 如何在 Symfony2 中正确使用 webSockets 2021-01-01
- PHP foreach() 与数组中的数组? 2022-01-01
- Laravel 5:Model.php 中的 MassAssignmentException 2021-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- openssl_digest vs hash vs hash_hmac?盐与盐的区别HMAC? 2022-01-01
- 如何使用 Google API 在团队云端硬盘中创建文件夹? 2022-01-01
- 使用 GD 和 libjpeg 支持编译 PHP 2022-01-01
- 覆盖 Magento 社区模块控制器的问题 2022-01-01