【问题标题】:Paginating results from model function模型函数的分页结果
【发布时间】:2014-12-26 22:26:35
【问题描述】:

对于 CakePhp 2.5

我的控制器中有以下搜索功能。

 public function search($query = null, $lim = null)
{
    $tokens = explode(" ", $query);
         $this->Paginator->settings = array(
                    'conditions' => array(
                        'Customer.site_id LIKE' => '%' . $this->viewVars['shopId'],
                        'CONCAT(Customer.first_name," ",Customer.last_name," ",Customer.organisation) LIKE' => '%' . implode(' ', $tokens) . '%'
                    ),
                    'limit' => $lim
                );
         $this->set('customers', $this->Paginator->paginate());
}

这工作正常,并得到我想要的结果。

但是,有人建议我将这些搜索功能放入我的模型中。我可以很容易地做到这一点,并且在我的模型中做了如下类似的操作:

 public function getActiveTasks($id){
        return $this->Task->find('all', array(
            'conditions' => array(
                'Task.customer_id' => $id,
                'Task.status' => 0
            ),
            'order' => array('Task.due_date ASC'),
            'recursive' => -1,
        ));
    }

我遇到的问题是我不能(或不知道如何)将 Paginator 与自定义搜索一起使用。有没有办法对自定义函数的结果进行分页?

即,我可以执行以下操作吗:

 public function CustomModelSearch($query = null, $lim = null)
{
    return $this->Task->find('all', array(
        'conditions' => array(
            'Customer.site_id LIKE' => '%' . $this->viewVars['shopId'],
            'CONCAT(Customer.first_name," ",Customer.last_name," ",Customer.organisation) LIKE' => '%' . implode(' ', $tokens) . '%'
        ),
        'limit' => $lim
    ));
}

然后在控制器中调用

$results = $this->Model->CustomModelSearch($query, $lim);
$this->set('results',$this->Paginate($results));

我找不到将结果传递到分页器以将分页结果呈现给视图的方法,除非我按照第一段代码通过控制器使用它,我被告知这不是好的 MVC 原则。

【问题讨论】:

    标签: cakephp pagination


    【解决方案1】:

    要创建自定义分页,您有两种选择:

    • 使用paginatepaginateCount 函数创建Custom Query Pagination

      // paginate 和 paginateCount 在行为上实现。 公共函数分页(模型 $model, $conditions, $fields, $order, $limit, $page = 1, $recursive = null, $extra = array()) { // 方法内容 }

      公共函数 paginateCount(模型 $model, $conditions = null, $recursive = 0, $额外=数组()){ // 方法体 }

    • 或创建custom find 并设置Paginator 以使用该查找。

    【讨论】:

      【解决方案2】:

      很好理解,但事实应该告诉我,错误会在你正在做的事情中打击你。

      “任务”模型中的函数你不应该把$this->task->findjust$this->find,如果你在模型中不需要指定你正在使用的模型,你只需要在控制器中这样做。

      模型/Task.php:

      public function CustomModelSearch($query = null, $lim = null)
      {
          return $this->find('all', array(
              'conditions' => array(
                  'Customer.site_id LIKE' => '%' . $this->viewVars['shopId'],
                  'CONCAT(Customer.first_name," ",Customer.last_name," ",Customer.organisation) LIKE' => '%' . implode(' ', $tokens) . '%'
              ),
              'limit' => $lim
          ));
      }
      

      【讨论】:

      • 我知道这一点。发布的功能只是说明我的观点的一个例子。
      • 把你的问题解释一下你的问题,这个错误给你和你的代码没有例子
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-05-31
      • 2017-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-10
      • 2012-03-12
      相关资源
      最近更新 更多