简体   繁体   English

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

[英]How can I get an “OR” condition within CakePHP?

I am trying to create a query that matches certain criteria OR matches an exact ID 我正在尝试创建一个符合某些条件的查询或一个完全匹配的ID

This is the query I want: 这是我想要的查询:

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

This is the query I get when debuggin: 这是调试时我得到的查询:

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

Here is my code: 这是我的代码:

$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());

I have also tried these combinations (along with a bunch of others) 我也尝试过这些组合(以及其他组合)

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

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

Also, why do you have return $conditions; 另外,为什么还要return $conditions; just after you create your $conditions array? 在创建$ conditions数组之后?

You need to put all the items you're OR'ing in the OR condition. 您需要将要进行“或”处理的所有项目置于“或”状态。 Also, that inner array around 'Listing.ID' => 75 should only necessary if you're doing on OR on the same field like Listing.ID = 75 OR Listing.ID = 25. Try the following (haven't tested it, obviously) 另外,仅当您在同一字段(例如Listing.ID = 75 OR Listing.ID = 25)上执行“或”操作时,“ Listing.ID” => 75周围的内部数组才应该是必需的。请尝试以下操作(未经测试) ,显然)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM