public static function updateAll($attributes, $condition = [])
{
$primaryKeys = static::primaryKeysByCondition($condition);
if (empty($primaryKeys)) {
return 0;
}
$bulkCommand = static::getDb()->createBulkCommand(["index" => static::index(), "type" => static::type()]);
foreach ($primaryKeys as $pk) {
$bulkCommand->addAction(["update" => ["_id" => $pk]], ["doc" => $attributes]);
}
$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.', $errors);
}
return $n;
}