Browse Source

Fixed storing and loading null-values in Redis

tags/2.0.0-rc
Alexander Makarov 10 years ago
parent
commit
660fa46127
  1. 17
      extensions/redis/ActiveQuery.php
  2. 45
      extensions/redis/ActiveRecord.php

17
extensions/redis/ActiveQuery.php

@ -123,6 +123,15 @@ class ActiveQuery extends Component implements ActiveQueryInterface
for ($i = 0; $i < $c;) {
$row[$dataRow[$i++]] = $dataRow[$i++];
}
$class = $this->modelClass;
$modelInstance = $class::instantiate($row);
foreach ($modelInstance->attributes() as $attribute) {
if (!isset($row[$attribute])) {
$row[$attribute] = null;
}
}
$rows[] = $row;
}
if (!empty($rows)) {
@ -164,6 +173,14 @@ class ActiveQuery extends Component implements ActiveQueryInterface
}
if ($this->asArray) {
$model = $row;
$class = $this->modelClass;
$modelInstance = $class::instantiate($row);
foreach($modelInstance->attributes() as $attribute) {
if (!isset($model[$attribute])) {
$model[$attribute] = null;
}
}
} else {
/* @var $class ActiveRecord */
$class = $this->modelClass;

45
extensions/redis/ActiveRecord.php

@ -119,12 +119,24 @@ class ActiveRecord extends BaseActiveRecord
$key = static::keyPrefix() . ':a:' . static::buildKey($pk);
// save attributes
$args = [$key];
$setArgs = [$key];
$delArgs = [$key];
foreach ($values as $attribute => $value) {
$args[] = $attribute;
$args[] = $value;
if ($value !== null) {
$setArgs[] = $attribute;
$setArgs[] = $value;
} else {
$delArgs[] = $attribute;
}
}
if (count($setArgs) > 1) {
$db->executeCommand('HMSET', $setArgs);
}
if (count($delArgs) > 1) {
$db->executeCommand('HDEL', $delArgs);
}
$db->executeCommand('HMSET', $args);
$changedAttributes = array_fill_keys(array_keys($values), null);
$this->setOldAttributes($values);
@ -158,26 +170,41 @@ class ActiveRecord extends BaseActiveRecord
$pk = static::buildKey($pk);
$key = static::keyPrefix() . ':a:' . $pk;
// save attributes
$args = [$key];
$delArgs = [$key];
$setArgs = [$key];
foreach ($attributes as $attribute => $value) {
if (isset($newPk[$attribute])) {
$newPk[$attribute] = $value;
}
$args[] = $attribute;
$args[] = $value;
if ($value !== null) {
$setArgs[] = $attribute;
$setArgs[] = $value;
} else {
$delArgs[] = $attribute;
}
}
$newPk = static::buildKey($newPk);
$newKey = static::keyPrefix() . ':a:' . $newPk;
// rename index if pk changed
if ($newPk != $pk) {
$db->executeCommand('MULTI');
$db->executeCommand('HMSET', $args);
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 {
$db->executeCommand('HMSET', $args);
if (count($setArgs) > 1) {
$db->executeCommand('HMSET', $setArgs);
}
if (count($delArgs) > 1) {
$db->executeCommand('HDEL', $delArgs);
}
}
$n++;
}

Loading…
Cancel
Save