From 660fa46127cc1d496f1cf903aba72ba2c328f8c7 Mon Sep 17 00:00:00 2001 From: Alexander Makarov Date: Sun, 7 Sep 2014 20:09:03 +0400 Subject: [PATCH] Fixed storing and loading null-values in Redis --- extensions/redis/ActiveQuery.php | 17 +++++++++++++++ extensions/redis/ActiveRecord.php | 45 +++++++++++++++++++++++++++++++-------- 2 files changed, 53 insertions(+), 9 deletions(-) diff --git a/extensions/redis/ActiveQuery.php b/extensions/redis/ActiveQuery.php index 65da8b3..1bf8f68 100644 --- a/extensions/redis/ActiveQuery.php +++ b/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; diff --git a/extensions/redis/ActiveRecord.php b/extensions/redis/ActiveRecord.php index fb02eb8..0151b05 100644 --- a/extensions/redis/ActiveRecord.php +++ b/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++; }