【问题标题】:How can I get an "OR" condition within CakePHP?如何在 CakePHP 中获得“或”条件?
【发布时间】:2013-11-15 17:36:51
【问题描述】:

我正在尝试创建一个匹配特定条件或匹配确切 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);

【问题讨论】:

    标签: php mysql sql cakephp cakephp-2.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;

    【讨论】:

    • 我已经更新了我想要的查询,以便您更好地了解我在寻找什么。
    • 好的,看看我的最新回答。
    【解决方案2】:

    您需要将所有要进行 OR 运算的项目置于 OR 条件中。此外,只有当您在 Listing.ID = 75 OR Listing.ID = 25 等相同字段上进行 OR 操作时,才需要围绕 'Listing.ID' => 75 的内部数组。尝试以下操作(尚未测试,显然)

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

    【讨论】:

      【解决方案3】:
      $conditions["AND"] = array( 'Listing.Color' => 'red',  Listing.Doors' => 2 );
      $conditions["OR"] = array( 'Listing.ID' => 75 );
      

      【讨论】:

      • 我已经更新了我想要的查询,以便您更好地了解我在寻找什么。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多