如何在Cakephp的两个不同日期之间过滤记录

时间:2014-05-21 12:59:37

标签: php sql cakephp solution

这是SQL

中的解决方案
SELECT * 
FROM  exploits
WHERE  date
BETWEEN  '2014-01-01'
AND  '2014-12-31'
ORDER BY  exploits.date DESC 
LIMIT 0 , 30

我在 CakePhp 中的解决方案(完全是Newbi)

$exploits = $this->Exploit->find('all', array(
               'conditions' => array(
               'Exploit.date BETWEEN 2014-01-01 AND 2014-12-31')
            $this->set('exploits', $exploits);

    }

感谢您的帮助

==

尝试新代码时

错误:发生了内部错误。

之前只有我拥有的东西 `private function _index_action(){

    $exploits = $this->Exploit->find('all', array(
        'conditions' => array(
            'is_published' => 1),
        'limit' => 6,
        'recursive' => -1,`

但我想尝试在日期之间只显示6个元素...(抱歉newbi和法语:P)

1 个答案:

答案 0 :(得分:2)

试试这段代码:

$exploits = $this->Exploit->find('all', array(
    'conditions' => array(
        'Exploit.date between ? and ?'=> array('2014-01-01', '2014-12-31'),
    ),
    'order' => array('date' => 'desc'),
    'limit'=>30
));