2014-09-20 50 views
31

什么是警予1.x中加入IN条件到活动查询 你可以使用CDbCriteria这样Yii2:活动记录添加未在条件

$cr = new CDbCriteria(); 
$cr->addNotInCondition('attribute', $array); 

的活动记录的方式似乎有不等价yii2活动记录实现中的API调用,如何通过活动记录执行此操作?

+0

此[链接](http://stackoverflow.com/questions/ 31041546/how-to-use-not-equal-to-inside-a-yii2-query/32860991#32860991)可以帮助某人。 – 2016-04-07 12:32:43

回答

64

那么所有的查询操作数似乎内yii\db\QueryInterface::Where()现合并每个文档 的状况现在可以使用的东西被添加像

$query = MyModel::find()->where(['attribute'=>$array]); 

的不是条件是稍有不同的格式

$query = MyModel::find()->where(['not in','attribute',$array]); 
+1

这会失败,我们需要:$ query = MyModel :: find() - > where(['attribute'=> $ array]) - > all(); – Billy 2015-12-07 20:07:22

+0

这将是一个条件,问题是关于不参考这里的细节:http://www.yiiframework.com/doc-2.0/yii-db-query.html#where()-detail – Manquer 2015-12-07 20:19:16

2

对于数字:

$query = MyModel::find()->where('NOT IN('.implode(',', $array).')'); 

对于字符串

$deleteContracts = Contract::find() 
    ->where([ 
     'session_id' => $session_id, 
     'status' => Contract::STATUS_COMPLETED 
    ]) 
    ->andWhere(['not in', 'contract_id', $contracts]) 
    ->all(); 
0

对我来说,唯一的工作解决办法是:

$query = MyModel::find()->where('`your-attribute` NOT IN(' . implode(',', $array) . ')')->all();