【问题标题】:Pagination in CakePHP omits fields existing in model, coincident with Datatable Component?CakePHP中的分页省略了模型中存在的字段,与Datatable Component一致?
【发布时间】:2013-07-20 12:13:51
【问题描述】:

有一种行为我不明白。

$test1 = $this->Post->find('all');
debug($test1);

它为该数组中的每个帖子提供预期的帖子数组,其中包含模型的所有字段:

id, field 1, field 2, field 3 ...

如果我使用以下语法:

$test2 = $this->paginate('Post');
debug($test2);

它仅显示预期的帖子数组,其中包含该数组中每个帖子的模型的某些特定字段:

id, field 2, field 5, field 14

为什么在使用paginate()-method 时会出现此输出?我如何为paginate()-method 获得与find()-method 相同的输出?

如果需要更多代码,请告诉我。

感谢任何帮助。

更新 1:

我发现我在这里加载的 DataTable-Component:https://github.com/cnizzdotcom/cakephp-datatable 可能会对这种奇怪的行为产生影响。但我还没有发现如何...

【问题讨论】:

  • 请检查您是否在 $paginate 初始化期间使用了fields 选项。
  • 不,就像: public $paginate = array( 'limit' => 5, 'maxLimit' => 25, 'order' => array( 'Post.DATE' => ' asc', ), );
  • 在你的 PostsController 中试试这个......$options = array('fields' => array('Post.*')); $this->Post->递归 = 0; $this->分页 = $options; $posts = $this->paginate();调试($posts);
  • @ScrappyCocco:我将您的确切代码粘贴到我的控制器中,但它仍然只提供一些字段。我必须扩展我的问题,因为我发现它肯定与我安装的数据表助手有关,它使用 und 更改 paginate() 本身。

标签: database cakephp controller pagination datatables


【解决方案1】:

我得到了解决方案。就像我认为问题出在 DataTable-Component 一样。在控制器中有多个 paginate() 调用,最后一个带有特定字段分配的调用保存在“$this->paginate”变量中。因此,当我在不设置字段的情况下运行 Cakes 正常分页时,会使用该变量,并且显然只设置我使用 DataTable 提供的字段。所以解决办法是设置

$this->paginate = array(
'fields' => array('Post.*')
);

在任何 paginate() 调用之前。感谢 Scrappy Cocco 提供星号提示。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-16
    • 1970-01-01
    • 2015-07-05
    • 2015-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-24
    相关资源
    最近更新 更多