0

I have a simple controller as following:

public function fetchDataAction($username){
    $user_email = $this -> getDoctrine()
        -> getRepository('AdminBundle:Users')
        -> findBy( array('username' => $username ), array('id' => 2));

    return $this -> render('AdminBundle:Admin:fetchData.html.twig', array('datas' => $user_email));
}

But in running the code I face the error:

Invalid order by orientation specified for AdminBundle\Entity\Users#userId

Passing one array to findBy() method, the code run errorlessly, But When I pass multiple arrays, It fails!

Where is the problem?

ahoora
  • 19
  • 4

1 Answers1

2

Problem is that second parameter to findBy is sort order. Passing array('id' => 2) as a sort order causes your error (if you've read it's text).

So the solution is to pass both filter criterias in one array:

-> findBy( array('username' => $username, 'id' => 2) );
u_mulder
  • 54,101
  • 5
  • 48
  • 64
  • I want records having `username = $username` OR records having `id = 2`. I think your recommendation give those records have `username = $username` AND records having `id = 2`. Am I right? – ahoora May 29 '17 at 09:22
  • Yes, you're right. In your case it is https://stackoverflow.com/questions/34003492/doctrine-findby-method-with-or-condition – u_mulder May 29 '17 at 10:26
  • Thanks but links didn't help! You know I do according to Symfony document http://symfony.com/doc/current/doctrine.html#fetching-objects-from-the-database but the result is not desirable. – ahoora May 30 '17 at 06:29