1

例如,我有一个包含 fromH 和 toH 字段的 Store 模型,我想从数据库中检索在给定时间 (@t) 开放的商店。

SQL 查询看起来像这样

select *
from store 
where 
( fromH < toH and @t between fromH and toH ) or
( fromH > toH and 
     (@t between fromH and 24 OR
     (@t between 1 and toH )
)

如何在 cakephp 中实现此查询,条件数组的外观如何?我想做蛋糕风格。

4

2 回答 2

1

您可以使用以下 CakePHP 等效选择查询来尝试它:

<?php $this->Store->find('all', array(
    'conditions' => array(
        'OR' => array(
            'AND' => array(
                'fromH < ' => 'toH',
                $t.' BETWEEN ? AND ?' => array('fromH', 'toH')
            )
        ),
    'AND' => array(
            'fromH > ' => 'toH',
            'OR' => array(
                $t.' BETWEEN ? AND ?' => array('fromH', '24'),
                $t.' BETWEEN ? AND ?' => array('1', 'toH')
            )
        ),
    )
));
于 2012-09-03T04:30:51.247 回答
1

@Arun Jain 给出的答案非常接近解决方案,但因为 OR 状态的规则必须每个都是它们自己的数组,因为它们的键值是相同的。如果你

print_r($conditions);

你会发现你的一些规则丢失了。

我认为以下解决方案对您有用。

$conditions = array(
    'conditions' => array(
        'OR' => array(
            array(
                'AND' => array(
                    'fromH <' => 'toH',
                    @t.' BETWEEN ? AND ?' => array('fromH','toH')
                 )
            ),
            array(
                'AND' => array(
                    'fromH >' => 'toH',
                    'OR' => array(
                        @t.' BETWEEN ? AND ?' => array('fromH','24'),
                        @t.' BETWEEN ? AND ?' => array('1','toH')
                    )
                )
            )
        )
    )
);

$this->Strore->find('all',$conditions);
于 2012-09-03T12:31:41.937 回答