订单条件在 cakephp 3 树行为中不起作用



我正在对类别表使用 cakephp-3 树行为。这里的"sequence_no"字段用于对类别的子类别列表进行排序。我正在搜索一个category_id,以按"sequence_no"的升序排列他的所有子类别。但是订单在这里不起作用。我的代码片段和输出在这里。

$categoris = $this->Categories->find('children', ['for' => $id])
->find('threaded')
->contain('ParentCategories')
->order(['Categories.sequence_no' => 'ASC'])
->toArray();

- 输出样本:

{
"status": "OK",
"result": {
"data": [
{
"id": 15,
"store_id": 0,
"uuid": null,
"name": "cat-3",
"parent_id": 3,
"lft": 16,
"rght": 17,
"sequence_no": 2,
"url": "cat-3",
"layout_id": 0,
"status": 1,
"total": 0,
"created": "2018-06-12T07:36:15+00:00",
"modified": "2018-06-12T08:15:12+00:00",
"parent_category": {
"id": 3,
"store_id": 2,
"uuid": null,
"name": "Pants",
"parent_id": null,
"lft": 15,
"rght": 20,
"sequence_no": null,
"url": "pants",
"layout_id": 0,
"status": 1,
"total": 0,
"created": "2018-06-06T10:23:50+00:00",
"modified": "2018-06-06T10:23:50+00:00"
},
"children": []
},
{
"id": 16,
"store_id": 0,
"uuid": null,
"name": "cat-4",
"parent_id": 3,
"lft": 18,
"rght": 19,
"sequence_no": 1,
"url": "cat-4",
"layout_id": 0,
"status": 1,
"total": 0,
"created": "2018-06-12T07:36:34+00:00",
"modified": "2018-06-12T08:15:12+00:00",
"parent_category": {
"id": 3,
"store_id": 2,
"uuid": null,
"name": "Pants",
"parent_id": null,
"lft": 15,
"rght": 20,
"sequence_no": null,
"url": "pants",
"layout_id": 0,
"status": 1,
"total": 0,
"created": "2018-06-06T10:23:50+00:00",
"modified": "2018-06-06T10:23:50+00:00"
},
"children": []
}
]
}

}

由于 findChildred 查找器在查询中添加了一个ORDER lft asc子句,因此您的订单条件将附加到该子句中

如果你想强制你的订单,你可以做

->order(['Categories.sequence_no' => 'ASC'], true)

order()方法中的第二个参数告诉 Cake 覆盖之前设置的ORDER BY

请参阅手册,关于段的结尾

最新更新