Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doctrine ordering toMany associations and matching Criteria

I've an entity with a OneToMany association. In this association I've defined an orderby and It works fine when I retrieve it.

class client {
    ...
    /**
    * @ORM\OneToMany(targetEntity="Reservation", mappedBy="client")
    * @ORM\OrderBy({"reservation_date" = "DESC"})
    */
    protected $reservations;

    ....

    public function getReservations()
    {
        return $this->reservations;
    }

    ....
}

The getReservations method works fine and It retrieve all Reservations ordered by the reservation_date field. But if I add this method:

public function getActiveReservations() {
    $activeCriteria = Criteria::create()
        ->where(Criteria::expr()->eq("active", true));

    return $this->getReservations()->matching($activeCriteria);
}

The matching criteria mess all results and are not ordered by the reservation_field.

How can I preserve the order after a matching criteria?

like image 916
Carlos Mayo Avatar asked Dec 18 '22 20:12

Carlos Mayo


1 Answers

As said in comments, you can use the Criteria::orderBy(array $orderings)

$activeCriteria = Criteria::create()
    ->where(Criteria::expr()->eq("active", true))
    ->orderBy(array('reservation_date' => Criteria::DESC));

Don't hesitate to read doctrine's source code if you can't find the answer in the documentation.

like image 55
chalasr Avatar answered May 12 '23 21:05

chalasr