首页 > 解决方案 > 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": []
        }
    ]
}

}

标签: cakephp-3.0

解决方案


由于 findChildred finderORDER lft asc在您的查询中添加了一个子句,您的订单条件将被附加到该查询中

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

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

方法中的第二个参数order()告诉 cake 覆盖ORDER BY之前的集合

见手册,关于段的结尾


推荐阅读