yii\db\ActiveRecord::updateAll PHP Method

updateAll() public static method

For example, to change the status to be 1 for all customers whose status is 2: php Customer::updateAll(['status' => 1], 'status = 2');
public static updateAll ( array $attributes, string | array $condition = '', array $params = [] ) : integer
$attributes array attribute values (name-value pairs) to be saved into the table
$condition string | array the conditions that will be put in the WHERE part of the UPDATE SQL. Please refer to [[Query::where()]] on how to specify this parameter.
$params array the parameters (name => value) to be bound to the query.
return integer the number of rows updated
    public static function updateAll($attributes, $condition = '', $params = [])
    {
        $command = static::getDb()->createCommand();
        $command->update(static::tableName(), $attributes, $condition, $params);
        return $command->execute();
    }

Usage Example

 /**
  * 重写,支持主键的缓存自动更新
  * @param array $attributes
  * @param string $condition
  * @param array $params
  * @return int
  */
 public static function updateAll($attributes, $condition = '', $params = [])
 {
     $ret = parent::updateAll($attributes, $condition, $params);
     if ($ret && self::allowFromCache($condition)) {
         $cache_key = self::getCacheKey($condition[static::$pk], true);
         Yii::trace('delete cache:' . $cache_key, __METHOD__);
         static::getCache()->delete($cache_key);
         $cache_key = self::getCacheKey($condition[static::$pk], false);
         static::getCache()->delete($cache_key);
     }
     return $ret;
 }