【问题标题】:CakePHP3.6: Paginator returns zero results despite find() functioning as usualCakePHP3.6:尽管 find() 正常运行,但分页器返回零结果
【发布时间】:2018-10-05 19:13:45
【问题描述】:

对此感到非常困惑;只需使用标准烘焙模板来 CRUD 访问我的模型。我正在从一个更旧版本的 cake 中迁移这个项目,但是到目前为止,我只是使用库存烘焙的控制器和模板。

/**
      * Index method
      *
      * @return \Cake\Http\Response|void
      */
     public function index() {
        $this->paginate = [
                'contain' => ['Associated1', 'Associated2', 'AssociatedEtc']
        ];
        $myModels = $this->paginate($this->MyModel);  // returns zero results
        $myModels_found = $this->MyModel->find('all'); // finds everything
        $this->set(compact('model'));
     }

CookBook 信息描述了以与最初烘焙控制器完全不同的方式实现 Paginator(并且尝试了那里描述的实现,得到了完全相同的结果)。

重要的是,就像在$myModel_found 中一样,如果我只是通过$model->find() 检索记录,一切都会照常返回,因此连接到数据层没有问题。

如何配置 Paginator 以便它实际检索我的记录?

更新

经过一番深入研究,我发现分页器只为具有非关联关联的模型返回零结果(即表 Model1 中的一行,对于 model2_id 等具有 0 或空值) t 返回。即使我删除了contain 条件,这种行为仍然存在。

【问题讨论】:

  • 请展示您的关联。您对他们使用哪种策略?我的意思是,如果你使用连接(对于 hasMany)并使用 INNER,你会得到预期的结果。

标签: cakephp cakephp-3.0


【解决方案1】:

**直到有时,甚至我都陷入了同样的境地。在我的控制器中,我有:

** /**

 * Index method
 *
 * @return \Cake\Http\Response|void
 */
public function index()
{
    $query = $this->request->query('query');
    $this->paginate = [
        'contain' => ['Customers', 'Drivers'],
        'limit'=>200,
        'order'=>[
            'pick_date'=>'desc'
        ]
    ];
    if(!empty($query)){
            $this->paginate = [
                'conditions'=>[
                    'destination LIKE' => '%'.$query.'%',
                    'starting_point LIKE' => '%'.$query.'%'
                    ]
            ];
        } 
    $rides =$this->paginate($this->Rides);
    $this->set(compact('rides'));
}

我犯的错误是在我的视图文件中:

<div class="form-group">
            <label class="col-md-3 control-label">Customer</label>
            <div class="col-md-4">
                <?= $this->Form->control('customer_id',array('class'=>'form-control input-circle', 'options'=>$customers, 'label'=>false)) ?>
            </div>
        </div>

在我的情况下,“customer_id”的拼写错误,因此结果没有显示在视图表中。但现在,一切正常。

请检查相关列名的所有拼写。

如果我在任何地方弄错了,请不要介意。我是 CakePHP 3 的学习者。

【讨论】:

  • 感谢您的努力,但这并不能解决我的问题;视图和控制器都是千篇一律的,没有错别字。
猜你喜欢
  • 1970-01-01
  • 2017-10-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-18
  • 1970-01-01
相关资源
最近更新 更多