public static function updateAll($attributes, $condition = null)
{
if (empty($attributes)) {
return 0;
}
$db = static::getDb();
$n = 0;
foreach (self::fetchPks($condition) as $pk) {
$newPk = $pk;
$pk = static::buildKey($pk);
$key = static::keyPrefix() . ':a:' . $pk;
$delArgs = [$key];
$setArgs = [$key];
foreach ($attributes as $attribute => $value) {
if (isset($newPk[$attribute])) {
$newPk[$attribute] = $value;
}
if ($value !== null) {
if (is_bool($value)) {
$value = (int) $value;
}
$setArgs[] = $attribute;
$setArgs[] = $value;
} else {
$delArgs[] = $attribute;
}
}
$newPk = static::buildKey($newPk);
$newKey = static::keyPrefix() . ':a:' . $newPk;
if ($newPk != $pk) {
$db->executeCommand('MULTI');
if (count($setArgs) > 1) {
$db->executeCommand('HMSET', $setArgs);
}
if (count($delArgs) > 1) {
$db->executeCommand('HDEL', $delArgs);
}
$db->executeCommand('LINSERT', [static::keyPrefix(), 'AFTER', $pk, $newPk]);
$db->executeCommand('LREM', [static::keyPrefix(), 0, $pk]);
$db->executeCommand('RENAME', [$key, $newKey]);
$db->executeCommand('EXEC');
} else {
if (count($setArgs) > 1) {
$db->executeCommand('HMSET', $setArgs);
}
if (count($delArgs) > 1) {
$db->executeCommand('HDEL', $delArgs);
}
}
$n++;
}
return $n;
}