Reputation: 237
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.
Upvotes: 21
Views: 133262
Reputation: 31
If you want and expect one result you can use findOneBy()
function.
$service = $repository->findOneBy(array('name' => 'Registration'),array('name' => 'ASC'),1 ,0)[0];
Upvotes: 3
Reputation: 364
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.
Upvotes: 26