如何申请&& 3 $条件之间[]?在cakephp

时间:2014-11-05 18:43:05

标签: cakephp

在Cakephp中,如何在这些行之间添加“AND”,$ conditions [] = codition1&& codition2&& condition3;

$conditions[]= array('Flight.from LIKE' => "%".$this->request->data['Flight']['from']."%");
$conditions[]= array('Flight.to LIKE' => "%".$this->request->data['Flight']['to']."%");
$conditions[]= array('Flight.date LIKE' => "%".$this->request->data['Flight']['date']."%");

2 个答案:

答案 0 :(得分:5)

您可以使用以下方法简化查询:

$conditions = array(
      'Flight.from LIKE' => "%".$this->request->data['Flight']['from']."%",
      'Flight.to LIKE' => "%".$this->request->data['Flight']['to']."%",
      'Flight.date LIKE' => "%".$this->request->data['Flight']['date']."%"
 );

当条件在不同的字段上时,您实际上并不需要围绕每个字段的数组。另外,CakePHP会在您的条件之间自动使用AND。

答案 1 :(得分:2)

$conditions = array(
    'AND' => array(
        'Flight.from LIKE' => "%".$this->request->data['Flight']['from']."%",
        'Flight.to LIKE' => "%".$this->request->data['Flight']['to']."%",
        'Flight.date LIKE' => "%".$this->request->data['Flight']['date']."%"
    )
);

官方文档和示例here