1

I have following repository:

    public function findClassPhotoByPath($path)
{
    return $this->getEntityManager()
        ->createQuery('SELECT p FROM KSRGalleryBundle:Photo p WHERE p.category = :category AND p.path = :path')
        ->setParameters(array('category' => 'class_photo', 'path' => $path))
        ->setMaxResults(1)
        ->getResult();
}

When I now use this repository-method the object is inside an array and I have to access it through $photo[0].

Is it possible to fetch the direct directly how it is the case by findOneBy()?

Best Regards, bodo

1 Answer 1

8

You can use getSingleResult(). Also check here for other options.

Edit:

If you don't want catch the NoResultException you can also use getOneOrNullResult() method.

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

2 Comments

Just be careful that one result is always obtained. An exception will be thrown for no results.
@Cerad, Right. This can be avoided using getOneOrNullResult() method.

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.