【问题标题】:How to pass custom mysql query in $this->paginate() in cakephp?如何在 cakephp 的 $this->paginate() 中传递自定义 mysql 查询?
【发布时间】:2011-12-25 14:35:59
【问题描述】:

作为 CAKEPHP 初学者,我正面临这个问题,如果这是一个非常简单的问题,我很抱歉。

我的数据库中的一个表中有一个 TIMESTAMP 字段,但我只想显示数据库中的月份和日期。由于有一个 mysql 函数 MONTHNAME() 和 DAYOFMONTH(),这将是我的解决方案,但我想知道如何使用 paginate() 函数来执行此操作,如下所示。

在我的 LogController index() 中,我有以下代码;

$this->Log->recursive = 0;
$this->paginate = array(
'limit' => 10,
'order' => array(
'Log.id' => 'desc'
));

$this->set('logs',$this->paginate());

在我的 View/Logs/index.ctp 中,我有;

$i = 0;
foreach ($logs as $log):
echo h($log["Log"]["time"]);

我该怎么做?

【问题讨论】:

    标签: cakephp pagination


    【解决方案1】:

    将字段添加到您的分页功能。

         $this->paginate = array(
            'limit' => 10,
            'order' => array(
                'Log.id' => 'desc'
            ),
            'fields' => '*,MONTHNAME(Log.time)');
    

    【讨论】:

      【解决方案2】:
      echo date('M d',$log["Log"]["time"]);
      

      【讨论】:

      • 谢谢,当我像 '$strtotime($log["Log"]["time"]);' 这样使用它时,这对我很有用。如果没有 strtotime,它会产生警告为“注意 (8):遇到格式不正确的数值 [APP\View\Logs\index.ctp,第 16 行]”。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多