代码之家  ›  专栏  ›  技术社区  ›  cck

CakePHP 3按关联模型排序,具有HasOne关系

  •  1
  • cck  · 技术社区  · 9 年前
    • 序有一个子序
    • 子目属于一个目

    我需要按子订单中的字段对订单进行排序,但在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)]);
            }
        ]);
    

    在里面 OrdersController.php 我有

        $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);
    

    在里面 index.ctp 我有

        $this->Paginator->sort('Suborder.created', 'Order Placed'),
        $this->Paginator->sort('Suborder.due_date'),
    

    Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Suborder.created' in 'order clause' .如何使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、orwhere进行修改,并基于多种条件进行连接。

    1 回复  |  直到 9 年前
        1
  •  4
  •   ndm    9 年前

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

    因此,您必须使用默认值 join 如果您想对其进行排序,请使用策略。

    另见