Stevan Tosic
Stevan Tosic

Reputation: 7229

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.

Upvotes: 1

Views: 670

Answers (2)

Rebangm
Rebangm

Reputation: 86

Your method getUser() doesn't exist in users entity.

Just iterate over your $datat and called method from user object like that

foreach ($users as $user) {
  // $user is an instance of users
 echo $user->getName(); //if this method exist in your entity model
}

Upvotes: 1

BENARD Patrick
BENARD Patrick

Reputation: 31005

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));

Upvotes: 3

Related Questions