如何在CakePHP中获得“OR”条件?

时间:2013-11-15 17:36:51

标签: php mysql sql cakephp cakephp-2.0

我正在尝试创建符合特定条件的查询或匹配确切的ID

这是我想要的查询:

SELECT * WHERE (`Listing.Color` = 'red' AND `Listing.Doors` = 2) OR Listing.ID = 75

这是我在调试时获得的查询:

SELECT * WHERE `Listing.Color` = 'red' AND `Listing.Doors` = 2 AND Listing.ID = 75

这是我的代码:

$conditions = array();

$conditions[] = array('Listing.Color' => 'red');
$conditions[] = array('Listing.Doors' => 4);

$conditions[] =  array( 
        "OR" => array(
            array('Listing.ID' => 75)
             ),
         );

return $conditions;

$this->paginate = array(
    'conditions' => $conditions,
);    


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

我也尝试过这些组合(以及其他一些组合)

$conditions["OR"][] = array('Listing.ID' => 75);
$conditions[]["OR"] = array('Listing.ID' => 75);
$conditions[]["OR"][] = array('Listing.ID' => 75);
$conditions["OR"][] = array('Listing.ID' => 75);

3 个答案:

答案 0 :(得分:1)

$conditions = array(
    'OR' => array(
         array('Listing.Color' => 'red', 'Listing.Doors' => 4),
         array('Listing.ID' => 75)
     )        
);

$this->paginate = array(
    'conditions' => $conditions,
);

另外,为什么在创建$ conditions数组后只有return $conditions;

答案 1 :(得分:0)

你需要将你正在进行OR的所有项目都置于OR状态。此外,'Listing.ID'周围的内部数组=>只有当你在相同的字段上对OR进行操作时才需要75.如上所示:ID = 75 OR Listing.ID = 25.请尝试以下操作(显然没有测试过)

$conditions[] =  array( 
    "OR" => array(
            'Listing.ID' => 75,
            'Listing.Doors' => 4,
         ),
     );

答案 2 :(得分:0)

$conditions["AND"] = array( 'Listing.Color' => 'red',  Listing.Doors' => 2 );
$conditions["OR"] = array( 'Listing.ID' => 75 );