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 Entity Class.
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 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 :)
Yes you can, usually using:
This should return you an array of entities.
If you want to get a single entity result, use either
getSingleResult
orgetOneOrNullResult
:Warning: These method can potentially throw
NonUniqueResultException
.Edit: Ok, so the question was about partial objects: http://docs.doctrine-project.org/en/latest/reference/partial-objects.html
getResult()
method returns a collection (an array) of entities. UsegetSingleResult()
if you're going to fetch only one object.EDIT:
Oh, I just noticed that you want to fetch a single field of a single object. Use
getSingleScalarResult()
as @Florian suggests.you can get an object instead of array by using "Partial Objects".
here is a tested example with DoctrineORM 2.2.2:
If you wish to return an object from your original query:
Remove this line
As soon as you use select, it will return an array...