Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony findOneBy / findBy

Has anyone face this strange issue with Symfony 3 (very last version)?

I have the following simple code:

$repository = $this->getDoctrine()
                   ->getManager()
                   ->getRepository('GeneralRegistrationBundle:Service');

$service = $repository->findOneBy(array('name' => 'Registration'),array('name' => 'ASC'));

$comment = $service->getComment();
$name = $service->getName();

return new Response('le service is '. $name . ', content is ' . $comment);

this code works.

I purge the cache and change findOneBy with findBy:

$service = $repository->findBy(array('name' => 'Registration'),array('name' => 'ASC'),1 ,0);

then I have the following error:

Error: Call to a member function getComment() on array

Is anybody have ideas or clues?

Thanks in advance.

like image 955
Dte Avatar asked Aug 07 '16 14:08

Dte


1 Answers

findBy() returns an array of objects with the given conditions.

It returns an empty array if none is found. If there is only one row satisfying your condition then you can add a [0] at the last of your $service like this:

$service = $repository->findBy(array('name' => 'Registration'),array('name' => 'ASC'),1 ,0)[0];

if not, you should loop through the found array with foreach or some thing similar.

like image 87
abdollah zakeri Avatar answered Jan 01 '23 23:01

abdollah zakeri