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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With