查询字符串的不同分页条件[CakePHP 3]

时间:2017-02-28 18:45:04

标签: php url cakephp cakephp-3.0

用户必须在URL中传入一个查询字符串,该字符串将返回不同的分页,如果他没有输入任何一个或两个查询,结果将会改变。下面的代码在CakePHP中运行,但它看起来很糟糕,有没有更好的方法来实现我想要做的事情,如果没有那些,否则如果条件?

if($this->request->query('date')) 
{ 
    $this->paginate = [ 
        'conditions' => [ 
          'Reservations.id_venue' => $this->Auth->user('id_venue_manager'), 
          'Reservations.date' => $this->request->query('date'), 
        ], 
    ]; 
} 
else if($this->request->query('id_user')) 
{ 
    $this->paginate = [ 
       'conditions' => [ 
          'Reservations.id_venue' => $this->Auth->user('id_venue_manager'), 
          'Reservations.id_user' => $this->request->query('id_user'), 
        ], 
    ]; 
} 
else if($this->request->query('id_user') && $this->request->query('date')) 
{ 
    $this->paginate = [ 
       'conditions' => [ 
          'Reservations.id_venue' => $this->Auth->user('id_venue_manager'), 
          'Reservations.id_user' => $this->request->query('id_user'), 
          'Reservations.date' => $this->request->query('date'), 
       ], 
    ]; 
} 
else 
{ 
    $this->paginate = [ 
       'conditions' => [ 
          'Reservations.id_venue' => $this->Auth->user('id_venue_manager'), 
       ], 
   ]; 
}

1 个答案:

答案 0 :(得分:0)

试试这个

<?php

public function someMethod() {
     $this->paginate = [ 
       'conditions' => $this->getPaginationConditions();
    ]; 
}

private function getPaginationConditions()
{
    $conditions = ['Reservations.id_venue' => $this->Auth->user('id_venue_manager')];  

    if ($date = $this->request->query('date')) {
        $conditions['Reservations.date'] = $date;
    }

    if ($user = $this->request->query('id_user')) {
        $conditions['Reservations.id_user'] = $user;
    }

    return $conditions;
}