-3

我想在 find('all' on cake 中输入一些条件。这是我的逻辑:

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转储

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 有点迷茫

4

2 回答 2

5

实际上,您开始时非常好(使用 OR 案例),但随后您将自己困在同一个老问题中:永远不要使用数组键两次(它只会覆盖前一个)!

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

基本上就是这样做的。

用这个:

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

您可以删除该'AND' =>部分,因为 cake 默认使用 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'),
)
于 2013-07-22T08:53:26.123 回答
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'))); 
于 2015-11-18T13:20:39.453 回答