Cakephp查找查询问题

时间:2013-03-28 10:10:32

标签: cakephp model cakephp-2.1

我正在对模型进行查找,如下所示,然而生成的SQL是错误的,因为我得到的结果不正确。

$bookings = $this->Booking->find('all', array(
    'conditions' => array(
        'Booking.created BETWEEN ? AND ?' => array(
            date('Y-m-d H:i:s', strtotime('-24 hours')),
            date('Y-m-d H:i:s', strtotime('-12 hours'))
        ),
        'OR' => array(
            'Booking.payment_status' => 3,
            'Booking.payment_status' => 2
        )
    )
));

此查找生成的条件是(如cake debugkit所示)

WHERE `Booking`.`created` BETWEEN '2013-03-27 11:01:57' AND '2013-03-27 23:01:57' AND `Booking`.`payment_status` = 2

我想要创建的条件是

WHERE `Booking`.`created` BETWEEN '2013-03-27 11:01:57' AND '2013-03-27 23:01:57' AND (`Booking`.`payment_status` = 2 OR `Booking`.`payment_status` = 3)

任何想法我在这里做错了什么。快速帮助将受到高度赞赏。谢谢!

1 个答案:

答案 0 :(得分:0)

经常造成PHP初学者错误 - 多次使用数组中的键:

    'OR' => array(
        'Booking.payment_status' => 3,
        'Booking.payment_status' => 2
    )

应该阅读

    'OR' => array(
        array('Booking.payment_status' => 3),
        array('Booking.payment_status' => 2)
    )