Stevan Tosic Stevan Tosic - 4 months ago 29
PHP Question

Doctrine findBy() calling method from entity

I need little help to call method from entity.

Here is the code I had try to execute.

$datat = $this->getDoctrine()
->getRepository('AppBundle:users')
->findBy(array('userId' => $userId));


after this, when I call

$data->getUser();


I get message about exeption "Error: Call to a member function getUser() on a non-object"

When I dump $data I got data from table or if I execute

->find()
with ID value.

Answer

findBy returns generally an ArrayCollection.

You should use findOneBy instead in order to target only one entity...

So :

$datat = $this->getDoctrine()  
   ->getRepository('AppBundle:users')  
   ->findOneBy(array('userId' => $userId));