找到所有的多个条件

时间:2013-07-22 08:47:08

标签: cakephp conditional-statements

我想在一个发现中输入一些条件('all'在蛋糕上。这是我的逻辑:

Traduction.traductions = my username AND Traduction.status = 1 OR 8    
OR,   Traduction.check1 = my username AND Traduction.status = 5 OR 2     
OR,   Traduction.check2 = my username AND Traduction.status = 6 OR 3    
Or,    Trauduction.editions = my username AND Traduction.status = 7 OR 3

如果其中一个条件是正确的,他会显示内容

到目前为止,这是我的代码

    $conditions = array(
                'AND' => array(
                    array(
                        'OR' => array(
                            array('Traduction.traductions' => $this->Session->read('Auth.User.username')),
                            array('Traduction.status' => '1'),
                            array('Traduction.status' => '8')
                        )
                    ),
                'AND' => array(
                    array(
                        'OR' => array(
                            array('Traduction.check1' => $this->Session->read('Auth.User.username')),
                            array('Traduction.check1' => '5'),
                            array('Traduction.check1' => '2')
                        )
                    )                       
                ),
                'AND' => array(
                    array(
                        'OR' => array(
                            array('Traduction.check2' => $this->Session->read('Auth.User.username')),
                            array('Traduction.check2' => '6'),
                            array('Traduction.check2' => '3')
                        )
                    )                       
                ),
                'AND' => array(
                    array(
                        'OR' => array(
                            array('Traduction.edition' => $this->Session->read('Auth.User.username')),
                            array('Traduction.edition' => '7'),
                            array('Traduction.edition' => '4')
                        )
                    )                       
                ),

            )
    );
    $traduction = $this->Traduction->find('all', array(
        'conditions' => $conditions

    ));

但遗憾的是它似乎无效。这里是de sql dump

SELECT `Traduction`.`id`, `Traduction`.`serie`, `Traduction`.`issue`,    
`Traduction`.`page`, `Traduction`.`writer`, `Traduction`.`drawer`, 
`Traduction`.`release`, `Traduction`.`traductions`, `Traduction`.`check1`, 
`Traduction`.`check2`, `Traduction`.`edition`, `Traduction`.`user_name`, 
`Traduction`.`content`, `Traduction`.`status` FROM 
`dcomicstrad`.`traductions` AS 
`Traduction` WHERE ((((`Traduction`.`traductions` = 'DrWhat') 
OR (`Traduction`.`status` = 1) 
OR (`Traduction`.`status` = 8))) 
AND (((`Traduction`.`edition` = 'DrWhat') 
OR (`Traduction`.`edition` = '7') 
OR (`Traduction`.`edition` = '4'))))

有人可以帮助我,我有点迷失所有这些阵列X_X

2 个答案:

答案 0 :(得分:5)

你实际上开始非常好(使用OR情况),但是你陷入了同样的问题:永远不要使用数组键两次(它只会覆盖前一个)!

$conditions = array(
    'AND' => array(),
    'AND' => array(),
    'AND' => array(),

基本上就是这样。

使用此:

$conditions = array(
     array(),
     array(),
     array(),

您可以删除'AND' =>部分,因为默认情况下使用AND来连接阵列数组。

如果你想保持AND,你需要让它更深一点:

$conditions = array(
    'AND' => array(
         array(),
         array(),
         array(),

作为旁注:我们正在讨论的仍然是CakePHP。因此,至少含有一点魔力,让你的生活更轻松。你可以在这里简化你的查询。每个OR语句都可以用较短的数组替换:

'OR' => array(
    array('Traduction.traductions' => $this->Session->read('Auth.User.username')),
    array('Traduction.status' => '1'),
    array('Traduction.status' => '8')
)

可以成为

'OR' => array(
    array('Traduction.traductions' => $this->Session->read('Auth.User.username')),
    array('Traduction.status' => array('1', '8'),
)

答案 1 :(得分:0)

if (!empty($this->data['Report']['ipno'])) 
        {
            $condition['DrugissueBatchIssue.patient_no like'] = "%".$this->data['Report']['ipno']."%";   
        }
        if (!empty($this->data['Report']['pname'])) 
        {
            $condition['DrugissueBatchIssue.patient_name like'] = "%".$this->data['Report']['pname']."%";   
        }             
        if (!empty($this->data['Report']['fromDate'])) 
        {
            $condition['date(DrugissueBatchIssue.created_date) >='] = date('Y-m-d',strtotime($this->data['Report']['fromDate'])); 
        }
        if (!empty($this->data['Report']['toDate'])) 
        {
            $condition['date(DrugissueBatchIssue.created_date) <='] = date('Y-m-d',strtotime($this->data['Report']['toDate']));             
        }
 $patient_wise_sales_report = $this->DrugissueBatchIssue->find('all', array('conditions'=>array($condition),'order'=>array('DrugissueBatchIssue.patient_no asc')));