bodokaiser
bodokaiser

Reputation: 15752

Doctrine fetch one object directly not in array

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

Upvotes: 1

Views: 3640

Answers (1)

Mun Mun Das
Mun Mun Das

Reputation: 15002

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.

Upvotes: 8

Related Questions