在yii2中实现或者where条件找到

时间:2016-08-11 12:01:49

标签: php yii2

我在yii2中执行了一个find()我明白可以有什么地方但是什么是orWher

我试过了

$query = Tblpr::find()->where(['PRID'=>2])->andwhere(['new'=>1])->all();

我如何实现orWhere

3 个答案:

答案 0 :(得分:2)

使用Where OR

$query = Tblpr::find();
$query->andFilterWhere(['or',
                    ['PRID',2],
                    ['new',1]
                  ])->all();

OR

$query = Tblpr::find()->select('*')
          ->orWhere(['PRID'=>2,'new'=>1])->all();

答案 1 :(得分:2)

您也可以使用createCommand

$query = (new \yii\db\Query())
                ->select('*')
                ->from('Tblpr') // put your table name here
                ->where(['PRID'=>[2]])
                ->orWhere(['new'=>[1]]);
        $command = $query->createCommand();
        print_r ($command->sql);die;

答案 2 :(得分:1)

以下内容适用于->where()->orWhere()

的查询
$query = Tblpr::find()
    ->where(['PRID' => 2])
    ->orWhere(['attribute' => 'value'])
    ->all();