0

I'm using a personal request in symfony witch is:

public function findByReferenceUser($Reference, $User)
{
    $qb = $this->createQueryBuilder('a')
        ->select('a')
        ->join('a.Reference', 'r')
        ->where('r.id = :refId')
        ->setParameter("refId", $Reference->getId())
        ->join('a.User', 'u')
        ->andWhere('u.id = :userId')
        ->setParameter("userId", $User->getId());

    return $qb->getQuery()->getOneOrNullResult();
}

But it doesn't work properly. I'm getting 3 results witch are of type NULL,NULL, Boolean. I'm using this to check it:

    $list_article = $RArticle->findByReferenceUser($reference, $panier->getUser());
    foreach ($list_article as $key => $article)
        echo gettype($article);

My database works, and have the right informations. Finnaly, this works:

    $list_article = $RArticle->findAll();
    foreach ($list_article as $key => $article)
        echo gettype($article);

And print object, object.

So there is my questions: Why do I get NULL, NULL, Boolean in the first case and how do I fixe it?

Thank you for your help! :)

3
  • try to change $qb->getQuery()->getOneOrNullResult(); to $qb->getQuery()->getResult(); Commented Jul 28, 2014 at 8:19
  • That's not what I'm looking for because I do want only one result witch in my case should not be null. Also, OneOrNull should set $list_article to null but it doesn't so it might not be the problem. Commented Jul 28, 2014 at 8:26
  • ok, try to print output of the $list_article with var_dump (whitout foreach).. Commented Jul 28, 2014 at 8:29

2 Answers 2

1

When using getOneOrNullResult, Doctrine will Retrieve a single object. If no object is found null will be returned. here.

If you want several object, you should use getResult

Sign up to request clarification or add additional context in comments.

Comments

0

findAll() returns array of objects, such as array('key' => 'object'), that's why you are getting correct records, getOneOrNullResult() returns single object (not array, foreach won't work), if you want to retrieve multiple records you need to use getResult()

1 Comment

You're right! I'm might be tied cause I knew it... So yes, the result is directly an object so no need to use foreach. Thank for the help. ^^

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.