原则对许多关联和匹配条件的排序

Car*_*ayo 1 symfony doctrine-orm

我有一个具有OneToMany关联的实体。在这个关联中,我定义了一个orderby,并且在检索它时可以正常工作。

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

    ....

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

    ....
}
Run Code Online (Sandbox Code Playgroud)

getReservations方法可以正常工作,并且可以检索字段中所有Reservations有序的对象reservation_date。但是,如果我添加此方法:

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

    return $this->getReservations()->matching($activeCriteria);
}
Run Code Online (Sandbox Code Playgroud)

匹配条件会将所有结果弄乱,并且不会按排序reservation_field

符合条件后如何保存订单?

cha*_*asr 5

如评论中所述,您可以使用 Criteria::orderBy(array $orderings)

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

教义非常完整,也不要犹豫打开类,如果您也找不到非常完整的文档中的答案,请尝试。