Search code examples
phpsymfonydoctrine-ormdoctrine

Doctrine findBy where and order


I have a snippet on symfony doctrine that picks data in descending order. Attempting to Apply a where clause some fields it equal to true is a problem. Below is my snippet

$results = $this->getDoctrine()->getRepository('RealBundle:Foo')->findBy([], ['id' => 'DESC','active' => true]); 

I have a field called active. Retrieving all the results where active is equalto true is a challenge

The above attempt gives an error

Invalid order by orientation specified for RealBundle\Entity\Foo#active


Solution

  • The first parameter is the WHERE clause, the second parameter is the ORDER.

    $results = $this
      ->getDoctrine()
      ->getRepository('RealBundle:Foo')
      ->findBy(['active'=>true], ['id' => 'DESC']); 
    

    findBy signature as described in the documentation

    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)