【问题标题】:Custom query pagination in cakephpcakephp中的自定义查询分页
【发布时间】:2018-07-13 11:01:19
【问题描述】:

我在具有多个连接的控制器中有如下查询

$deals = $this->Deals->find('all', array(
    'fields' => array(
        'Deals.id',
        'Deals.deed_amount',
        'Deals.other_amount',
        'Deals.cash_amount',
        'Deals.total_amount',
        'Deals.client_id',
        'Deals.project_id',
        'Deals.property_id',
        'Deals.properties_flat_id',
        'Deals.date',
        'Deals.status',
        'Deals.invoice_no',
        'Deals.remain_cash_amount',
        'Deals.remain_deed_amount',
        'Deals.remain_other_amount',
        'Deals.remain_deed_amount',
        'Property.name',
        'Client.name',
        'PropertyFlat.name',
        'PropertyFlat.status'


    ),
    'joins' => array(
        array(
            'table' => 'properties',
            'alias' => 'Property',
            'type'  => 'LEFT',
            'conditions' => array(
                'Deals.property_id = Property.id'
            )
        ),
        array(
            'table' => 'clients',
            'alias' => 'Client',
            'type'  => 'LEFT',
            'conditions' => array(
                'Deals.client_id = Client.id'
            )
        ),
        array(
            'table' => 'properties_flats',
            'alias' => 'PropertyFlat',
            'type'  => 'LEFT',
            'conditions' => array(
                'Deals.properties_flat_id = PropertyFlat.id'
            )
        )

    ),

   // 'conditions'=>$condition


));

我想为视图添加分页。我试过了

$this->set('deals', $this->Paginator->paginate($deals));

但是没有用。请帮助我如何为自定义查询添加分页。我搜索了很多链接,但没有得到任何帮助。

【问题讨论】:

标签: php sql cakephp


【解决方案1】:

你可以这样试试:

       $paginate = [
                        'joins' =>  array(
                array(
                    'table' => 'properties',
                    'alias' => 'Property',
                    'type'  => 'LEFT',
                    'conditions' => array(
                        'Deals.property_id = Property.id'
                    )
                ),
                array(
                    'table' => 'clients',
                    'alias' => 'Client',
                    'type'  => 'LEFT',
                    'conditions' => array(
                        'Deals.client_id = Client.id'
                    )
                ),
                array(
                    'table' => 'properties_flats',
                    'alias' => 'PropertyFlat',
                    'type'  => 'LEFT',
                    'conditions' => array(
                        'Deals.properties_flat_id = PropertyFlat.id'
                    )
                )

            ),
    'fields' => array(
            'Deals.id',
            'Deals.deed_amount',
            'Deals.other_amount',
            'Deals.cash_amount',
            'Deals.total_amount',
            'Deals.client_id',
            'Deals.project_id',
            'Deals.property_id',
            'Deals.properties_flat_id',
            'Deals.date',
            'Deals.status',
            'Deals.invoice_no',
            'Deals.remain_cash_amount',
            'Deals.remain_deed_amount',
            'Deals.remain_other_amount',
            'Deals.remain_deed_amount',
            'Property.name',
            'Client.name',
            'PropertyFlat.name',
            'PropertyFlat.status'


        )
   ];
 $this->set('deals', $this->Paginator->paginate($this->Deals->find('all'), $paginate)->toArray());

【讨论】:

    【解决方案2】:

    首先,我建议您重新考虑构建查询的方式。为什么不直接使用 contains() 方法来检索相关数据?

    其次,您可以将自定义查找器与分页器一起使用。只需在您的 Table 类中创建一个并从中返回构造查询并要求分页器使用它。在这里阅读更多。 https://book.cakephp.org/3.0/en/controllers/components/pagination.html

    【讨论】:

      猜你喜欢
      • 2010-12-23
      • 1970-01-01
      • 1970-01-01
      • 2015-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多