18

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 returns as array. Is it possible to fetch it with Entity Manager Class? If yes, how?

I digg the documentation but it seems not possible or not exist in the doc or I'm just blind :)

yAnTar
  • 4,269
  • 9
  • 47
  • 73
flower58
  • 687
  • 2
  • 8
  • 15

4 Answers4

30

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 getSingleResult or getOneOrNullResult:

$repository
    ->createQueryBuilder('p')
    ->getQuery()
    ->getOneOrNullResult()
;

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

Florian Klein
  • 8,692
  • 1
  • 32
  • 42
18

you can get an object instead of array by using "Partial Objects".

here is a tested example with 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
ismaail E.G.
  • 419
  • 5
  • 16
10

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...

HappyCoder
  • 5,985
  • 6
  • 42
  • 73
  • The question-related code itself was about loading as less data as possible so I suppose it's not an appropriate solution. – igronus Feb 17 '22 at 16:34
4

getResult() method returns a collection (an array) of entities. Use getSingleResult() 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.

Community
  • 1
  • 1
Crozin
  • 43,890
  • 13
  • 88
  • 135
  • No, i will fetch few columns not one. The query in this question is just an example. Not all rows and not one row. – flower58 May 07 '12 at 12:35
  • 2
    I think you mix the idea of column, row and entity. What @Crozin says is right. – Florian Klein May 07 '12 at 12:39
  • Moreover, If you want to fetch only one column, then use `getSingleScalarResult`. – Florian Klein May 07 '12 at 12:40
  • @Florian, Yes and no. The aquery in this question is just an example. Lets say i want to fetch 4 column. But i dont want to fetch with arrays i want to fetch with Entity Class? Like `$result->getColumn();` instead of `$result['column'];` I am missing something here so lighting me. :) – flower58 May 07 '12 at 12:43
  • Ha, ok :) I think you cannot hydrate an entity if you dan't have all the columns in the resultset. – Florian Klein May 07 '12 at 12:45
  • 1
    Using `QueryBuilder`, it should always fetch all columns for a given entity. – Florian Klein May 07 '12 at 12:46
  • If you really want to partially hydrate your entity, use http://docs.doctrine-project.org/projects/doctrine-orm/en/2.0.x/reference/partial-objects.html – Florian Klein May 07 '12 at 12:47