只需將您心愛的屬性添加回來,並使用Table對象中的beforeFind()回調函數將該屬性中的值添加到查詢中即可。
或者只是create a custom finder:
public function findOrdered(Query $query, $options) {
return $query->order([
$this->alias() . '.name' => 'ASC'
]);
}
而且使用它
$this->find('list')->find('ordered')->all();
或創建一個有序列表發現,返回排序列表中的全部。
public function findOrderedList(Query $query, $options) {
return $this->findList($query, $options)
->order([
$this->alias() . '.name' => 'ASC'
]);
}
或直接重載findList()方法並調用父類。
或者如果您的find()
通過關係被調用,則可以使用sort
選項set the default order for the relationship。
$this->hasMany('AuditLogs', [
'sort' => [
'AuditLogs.timestamp' => 'desc',
],
]);
我建議你'$這個 - > aliasField(」 name')'而不是' - > alias()'連接' –
@burzum有沒有你說過的beforeFind選項的例子? – khany
https://github.com/UseMuffin/Orderly – ADmad