4

我想用多个调用在哪里构建查询,但是在使用此代码时出现错误

$command = Yii::app()->db->createCommand()
    ->select('*')
    ->from('{{table}}');

$command->where('value1 = :value1', array(':value1' => 1));
$command->where('value2 < :value2', array(':value2' => 2));

我明白,我可以使用类似的代码

$command->where('value1 = :value1 AND value2 = :value2', array(':value1' => 1, ':value2' => 2));

但我有困难的条件和更简单的使用代码,如上。

在 Codeigniter 中,我可以多次使用这些条件

$this->db->where()
4

2 回答 2

6

你必须给它传递一个这样的数组:

$command->where(array('AND', 'value1 = :value1', 'value2 < :value2'), array(':value1' => 1, ':value2' => 2));
于 2012-07-19T15:26:26.587 回答
0

实际上我找到了一个更好的方法来做到这一点

$result = array();
    $result = Yii::app()->db->createCommand()
            ->select('*')
            ->from('table');

    $condition = array();
    $cond_arg = array();

    if (!empty($email)) {

        $condition[] = "email =:email";
        $cond_arg[':email'] = $email;
    }

    if (!empty($value2)) {

        $condition[] = "value2 =:value2";
        $cond_arg[':value2'] = $value2;
    }

    if (!empty($value3)) {

        $condition[] = "value3 like :value3";
        $cond_arg[':value3'] = $value3;
    }


    if (count($condition) > 0) {
        $result->where(array('AND', implode(" AND ", $condition)), $cond_arg);
    }

    return $result->queryAll();
于 2014-06-09T00:39:26.947 回答