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

45
extensions/redis/ActiveRecord.php

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

Loading…
Cancel
Save