CakePHP 3通过关联模型进行排序,具有一个关系 订单有一个子订单 子订单属于订单

CakePHP 3通过关联模型进行排序,具有一个关系 订单有一个子订单 子订单属于订单,cakephp,pagination,cakephp-3.x,Cakephp,Pagination,Cakephp 3.x,我需要按子订单中的字段对订单进行排序,但在Cake 3.x中,按虚拟字段排序似乎已被删除 在OrdersTable.php中,我有 $this->hasOne('Suborder', [ 'className' => 'Suborders', 'foreignKey' => 'order_id', 'strategy' => 'select', 'conditions' => function

我需要按子订单中的字段对订单进行排序,但在Cake 3.x中,按虚拟字段排序似乎已被删除

OrdersTable.php
中,我有

    $this->hasOne('Suborder', [
        'className' => 'Suborders',
        'foreignKey' => 'order_id',
        'strategy' => 'select',
        'conditions' => function ($exp, $query) {
            return $exp->add(['Suborder.id' => $query
                ->connection()
                ->newQuery()
                ->select(['SSO.id'])
                ->from(['SSO' => 'suborders'])
                ->where([
                    'Suborder.order_id = SSO.order_id',
                    'SSO.suborder_type_id in' => [1, 2, 3]
                ])
                ->order(['SSO.id' => 'DESC'])
                ->limit(1)]);
        }
    ]);
    $this->paginate = [
        'limit' => 20,
        'order' => ['id' => 'desc'],
        'sortWhitelist' => [
            'id',
            'title',
            'client_order',
            'substatus',
            'Workflows.order_status_id',
            'Clients.name',
            'ProductTypes.type',
            'Suborder.due_date',
            'Suborder.created',
        ],
    ];

    $orders = $this->paginate($collection);
    $this->Paginator->sort('Suborder.created', 'Order Placed'),
    $this->Paginator->sort('Suborder.due_date'),
OrdersController.php
中,我有

    $this->hasOne('Suborder', [
        'className' => 'Suborders',
        'foreignKey' => 'order_id',
        'strategy' => 'select',
        'conditions' => function ($exp, $query) {
            return $exp->add(['Suborder.id' => $query
                ->connection()
                ->newQuery()
                ->select(['SSO.id'])
                ->from(['SSO' => 'suborders'])
                ->where([
                    'Suborder.order_id = SSO.order_id',
                    'SSO.suborder_type_id in' => [1, 2, 3]
                ])
                ->order(['SSO.id' => 'DESC'])
                ->limit(1)]);
        }
    ]);
    $this->paginate = [
        'limit' => 20,
        'order' => ['id' => 'desc'],
        'sortWhitelist' => [
            'id',
            'title',
            'client_order',
            'substatus',
            'Workflows.order_status_id',
            'Clients.name',
            'ProductTypes.type',
            'Suborder.due_date',
            'Suborder.created',
        ],
    ];

    $orders = $this->paginate($collection);
    $this->Paginator->sort('Suborder.created', 'Order Placed'),
    $this->Paginator->sort('Suborder.due_date'),
index.ctp
中,我有

    $this->hasOne('Suborder', [
        'className' => 'Suborders',
        'foreignKey' => 'order_id',
        'strategy' => 'select',
        'conditions' => function ($exp, $query) {
            return $exp->add(['Suborder.id' => $query
                ->connection()
                ->newQuery()
                ->select(['SSO.id'])
                ->from(['SSO' => 'suborders'])
                ->where([
                    'Suborder.order_id = SSO.order_id',
                    'SSO.suborder_type_id in' => [1, 2, 3]
                ])
                ->order(['SSO.id' => 'DESC'])
                ->limit(1)]);
        }
    ]);
    $this->paginate = [
        'limit' => 20,
        'order' => ['id' => 'desc'],
        'sortWhitelist' => [
            'id',
            'title',
            'client_order',
            'substatus',
            'Workflows.order_status_id',
            'Clients.name',
            'ProductTypes.type',
            'Suborder.due_date',
            'Suborder.created',
        ],
    ];

    $orders = $this->paginate($collection);
    $this->Paginator->sort('Suborder.created', 'Order Placed'),
    $this->Paginator->sort('Suborder.due_date'),
我得到的错误是:
error:SQLSTATE[42S22]:未找到列:1054未知列“Suborder.created”在“order子句”中
。如何让Cake在初始查询中包含子订单以进行排序和分页

编辑:

    $collection = $this->Orders->find()
        ->contain([
            'Clients',
            'CurrentAssignment.Users',
            'Workflows.OrderStatuses.Category',
            'Workflows.OrderStatuses.Departments' => function ($q) use ($uID) {
                return $this->Departments->find()->matching('Users', function ($q) use ($uID) {
                    return $q->where(['Users.id' => $uID]);
                });
            },
            'ClientProducts.ProductTypes',
            'Reviews' => function ($q) {
                return $q->where(['review_type_id is not' => 6]);
            },
            'Reviews.ReviewTypes',
            'PublicNotes',
            'ActiveReview',
            'Suborder',
            'Suborder.SuborderTypes',
            'Suborders.SuborderTypes',
        ]);

并且,
$collection
被修改为150行where,或where,并基于许多条件进行连接。

您已将关联配置为使用
选择
策略,该策略将使用单独的查询来检索数据(),因此您无法在用于分页的主查询中引用它

因此,如果要对其进行排序,则必须使用默认的
join
策略

另见


您已将关联配置为使用
select
策略,该策略将使用单独的查询来检索数据(),因此您无法在用于分页的主查询中引用它

因此,如果要对其进行排序,则必须使用默认的
join
策略

另见


虽然原因对于习惯使用CakePHP的人来说是显而易见的,但它仍然可能取决于
$collection
到底是什么!虽然原因对于习惯使用CakePHP的人来说是显而易见的,但它仍然可能取决于
$collection
到底是什么!