Doctrine querybuilder result
Asked Answered
L

2

6

I am developing an application using Symfony2 and Doctrine2. I also use Doctrine's QueryBuilder. I have this query:

public function getInterpDesberdinak($MarkId)
    {
          $qb = $this->createQueryBuilder('c')
              ->select('DISTINCT c.Gordailua, c')
              ->where('c.MarkIdGordailua = :MarkId')
              ->setParameter('MarkId', $MarkId);
          $Emaitza = $qb->getQuery()->getResult();
          return $Emaitza;
    }       

I would like to how the result would I get in $Emaitza would look. Would it be something like:

$Emaitza[0]['Gordailua'] = the first Gordailua value selected.

and then $Emaitza[0][?????] = The first c type object.

I am kind of confused.Thanks.

Lick answered 19/5, 2012 at 11:25 Comment(0)
P
-1

First of all you need to specify the From clause:

$qb->from('YourBundle\Entity\ParentOfGordailua as pg');

You can use the: getArrayResult() to fetch the data as an array, if you want get First object from the Object ArrayCollection(when you're using the getResult() you must:

$Gordailua = $Emaitza->first()->getGordailua();

Look at the reference and exaples: http://docs.doctrine-project.org/projects/doctrine-orm/en/2.0.x/reference/dql-doctrine-query-language.html

Perfunctory answered 20/5, 2012 at 10:17 Comment(2)
I don't think this is correct solution: $Emaitza = $qb->getQuery()->getResult(); this is an array, you can't call ->first() on an array.Ramburt
besides, if you call $this->createQueryBuilder from an EntityRepository the FROMclause is taken care ofSettee
H
11

Can't remember how I came to this post, but I even read it and thought, well .. anyways ..

I think he's asking to get the first result, if so .. the query should first know what the maximum size of result is by doing

$qb->setMaxResults(1)

and then you can call a method that really defines itself

$single_result = $qb->getSingleResult()

So in the example given the code will look like

$qb->getQuery()->setMaxResults(1)->getSingleResult();

And of courses, as stated below, if the query returns null, an error will be thrown, so here a version which verifies the content of the query

if($query = $db->getQuery())
  return $query->setMaxResults(1)->getSingleResult();
Hamal answered 22/10, 2014 at 19:32 Comment(1)
Problem here, is that this query will throw an error if your query returns nothing. Example, you querying "id = 1", if id = 1 not exists you will get the error "No result was found for query although at least one row was expected."Frodi
P
-1

First of all you need to specify the From clause:

$qb->from('YourBundle\Entity\ParentOfGordailua as pg');

You can use the: getArrayResult() to fetch the data as an array, if you want get First object from the Object ArrayCollection(when you're using the getResult() you must:

$Gordailua = $Emaitza->first()->getGordailua();

Look at the reference and exaples: http://docs.doctrine-project.org/projects/doctrine-orm/en/2.0.x/reference/dql-doctrine-query-language.html

Perfunctory answered 20/5, 2012 at 10:17 Comment(2)
I don't think this is correct solution: $Emaitza = $qb->getQuery()->getResult(); this is an array, you can't call ->first() on an array.Ramburt
besides, if you call $this->createQueryBuilder from an EntityRepository the FROMclause is taken care ofSettee

© 2022 - 2024 — McMap. All rights reserved.