ActiveRecord.php
public static function updateAllCounters($counters, $condition = [])
    {
        $primaryKeys = static::primaryKeysByCondition($condition);
        if (empty($primaryKeys) || empty($counters)) {
            return 0;
        }

        $bulkCommand = static::getDb()->createBulkCommand([
            "index" => static::index(),
            "type" => static::type(),
        ]);
        foreach ($primaryKeys as $pk) {
            $script = '';
            foreach ($counters as $counter => $value) {
                $script .= "ctx._source.{$counter} += {$counter};\n";
            }
            $bulkCommand->addAction(["update" => ["_id" => $pk]], ["script" => $script, "params" => $counters, "lang" => "groovy"]);
        }
        $response = $bulkCommand->execute();

        $n = 0;
        $errors = [];
        foreach ($response['items'] as $item) {
            if (isset($item['update']['status']) && $item['update']['status'] == 200) {
                $n++;
            } else {
                $errors[] = $item['update'];
            }
        }
        if (!empty($errors) || isset($response['errors']) && $response['errors']) {
            throw new Exception(__METHOD__ . ' failed updating records counters.', $errors);
        }

        return $n;
    }
public static function updateAllCounters($counters, $condition = [])
    {
        $primaryKeys = static::primaryKeysByCondition($condition);
        if (empty($primaryKeys) || empty($counters)) {
            return 0;
        }

        $bulkCommand = static::getDb()->createBulkCommand([
            "index" => static::index(),
            "type" => static::type(),
        ]);
        foreach ($primaryKeys as $pk) {
            $script = '';
            foreach ($counters as $counter => $value) {
                $script .= "ctx._source.{$counter} += {$counter};\n";
            }
            $bulkCommand->addAction(["update" => ["_id" => $pk]], ["script" => $script, "params" => $counters, "lang" => "groovy"]);
        }
        $response = $bulkCommand->execute();

        $n = 0;
        $errors = [];
        foreach ($response['items'] as $item) {
            if (isset($item['update']['status']) && $item['update']['status'] == 200) {
                $n++;
            } else {
                $errors[] = $item['update'];
            }
        }
        if (!empty($errors) || isset($response['errors']) && $response['errors']) {
            throw new Exception(__METHOD__ . ' failed updating records counters.', $errors);
        }

        return $n;
    }
public static function updateAllCounters ( $ counters , $ condition = [ ] ) { $ primaryKeys = static :: primaryKeysByCondition ( $ condition ) ; if ( empty ( $ primaryKeys ) || empty ( $ counters ) ) { return 0 ; } $ bulkCommand = static :: getDb ( ) -> createBulkCommand ( [ "index" => static :: index ( ) , "type" => static :: type ( ) , ] ) ; foreach ( $ primaryKeys as $ pk ) { $ script = '' ; foreach ( $ counters as $ counter => $ value ) { $ script .= "ctx._source.{$counter} += {$counter};\n" ; } $ bulkCommand -> addAction ( [ "update" => [ "_id" => $ pk ] ] , [ "script" => $ script , "params" => $ counters , "lang" => "groovy" ] ) ; } $ response = $ bulkCommand -> execute ( ) ; $ n = 0 ; $ errors = [ ] ; foreach ( $ response [ 'items' ] as $ item ) { if ( isset ( $ item [ 'update' ] [ 'status' ] ) && $ item [ 'update' ] [ 'status' ] == 200 ) { $ n ++ ; } else { $ errors [ ] = $ item [ 'update' ] ; } } if ( ! empty ( $ errors ) || isset ( $ response [ 'errors' ] ) && $ response [ 'errors' ] ) { throw new Exception ( __METHOD__ . ' failed updating records counters.' , $ errors ) ; } return $ n ; }
Updates all matching records using the provided counter changes and conditions.
For example, to add 1 to age of all customers whose status is 2,

~~~
Customer::updateAllCounters(['age' => 1], ['status' => 2]);
~~~

@param array $counters the counters to be updated (attribute name => increment value).
Use negative values if you want to decrement the counters.
@param array $condition the conditions that will be passed to the `where()` method when building the query.
Please refer to [[ActiveQuery::where()]] on how to specify this parameter.
@see [[ActiveRecord::primaryKeysByCondition()]]
@return int the number of rows updated
@throws Exception on error.
Updates all matching records using the provided counter changes and conditions . For example to add 1 to age of all customers whose status is 2