CakePHP复杂查找条件

CakePHP复杂查找条件,cakephp,cakephp-1.3,cakephp-2.0,cakephp-2.1,cakephp-1.2,Cakephp,Cakephp 1.3,Cakephp 2.0,Cakephp 2.1,Cakephp 1.2,例如,我有一个带有from和toH字段的存储模型,我想从数据库中检索在给定时间(@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 ) ) 选择* 从商店 哪里 (FromtoH和 (@t

例如,我有一个带有from和toH字段的存储模型,我想从数据库中检索在给定时间(@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 )
)
选择*
从商店
哪里
(FromtoH和
(@t介于从h到24之间或
(@t介于1和toH之间)
)

如何在cakephp中实现此查询,条件数组会是什么样子?我想用蛋糕式的方式实现它。

您可以使用以下cakephp等价的select查询进行尝试:

<?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')
            )
        ),
    )
));

由@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);
$conditions=array(
“条件”=>数组(
'或'=>数组(
排列(
'和'=>数组(
'fromH'=>'到',
'或'=>数组(
@t、 'BETWEEN?AND'=>数组('fromH','24'),
@t、 'BETWEEN?AND?'=>数组('1','toH')
)
)
)
)
)
);
$this->Strore->find('all',$conditions);

可能还需要修复双精度中间值。当我尝试运行此查询时,会出现以下错误:错误:SQLSTATE[42S22]:未找到列:1054 where子句中的未知列“15”-“15”是@t