批量修改
//修改ip为192.168.78.21的所有信息,将其country改为中国,area改为华北
$db = Month::updateAll([
'country' => '中国',
'area' => '华北',
], ['ip' => 192.168.78.21]);
Yii2 中的updateAll可以接受三个参数,
attributes,
condition,$params = null
第一个参数是要更新的值,第二个是条件,如果第二个条件中用了占位符,就必须 要有第三个条件。
平时我们查询 的时候用andWhere()可以连接很多参数,那么updateAll的时候,如果有多个参数怎么办?
Test::updateAll([‘status’=>1],[‘status’=>0,’flag’=>1]);
如果参数都是固定值,这样的写法当然OK,如果有范围值就只能这样写了,比如flag != 1怎么办?
Test::updateAll([‘status’=>1],[‘and’,[‘status’=>0],[‘<>’,’flag’,1]]);
PS:参考:http://www.yiiframework.com/doc-2.0/yii-db-query.html#where()-detail