cakephp-3.0 - ORDER 条件在 cakephp 3 树行为中不起作用
问题描述
我正在为类别表使用 cakephp-3 树行为。这里的 'sequence_no' 字段用于对类别的子类别列表进行排序。我正在使用 category_id 搜索以按“sequence_no”按 ASCENDING 顺序获取他的所有子类别。但是订单在这里不起作用。我的代码片段和输出在这里。
$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 finderORDER lft asc
在您的查询中添加了一个子句,您的订单条件将被附加到该查询中
如果你想强制你的订单,你可以做
->order(['Categories.sequence_no' => 'ASC'], true)
方法中的第二个参数order()
告诉 cake 覆盖ORDER BY
之前的集合
见手册,关于本段的结尾
推荐阅读
- java - 实现 Spring 拦截器
- linux-kernel - 从内核映射内存区域
- reactjs - 调用 this.setState 后状态不变
- c# - 如何将一个盒子放到鼠标位置?
- c# - 如何在我的 ASP.NET WebApp 中使用 Google Analytics
- php - 如何在 Laravel 中发出 Ajax 请求?
- ruby-on-rails - 如何向 Rails JSON 对象添加信息?
- latency - statsd 的副作用可能导致额外的延迟
- rust - rust libc::setsockopt 强制转换为 c_void
- c# - Unity 2017 - 气球生成器不起作用?