From cd3950aaa71ba30be210e012a633614a1f0bdc9a Mon Sep 17 00:00:00 2001 From: Paul Klimov Date: Mon, 18 Nov 2013 14:03:42 +0200 Subject: [PATCH] Sphinx Active Record saving advanced to use 'replace' for runtime indexes --- extensions/sphinx/ActiveRecord.php | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/extensions/sphinx/ActiveRecord.php b/extensions/sphinx/ActiveRecord.php index 58325c6..5256c1c 100644 --- a/extensions/sphinx/ActiveRecord.php +++ b/extensions/sphinx/ActiveRecord.php @@ -850,20 +850,30 @@ class ActiveRecord extends Model $this->afterSave(false); return 0; } - $condition = $this->getOldPrimaryKey(true); - $lock = $this->optimisticLock(); - if ($lock !== null) { - if (!isset($values[$lock])) { - $values[$lock] = $this->$lock + 1; + + if ($this->getIndexSchema()->isRuntime) { + $values = array_merge($values, $this->getOldPrimaryKey(true)); + $command = static::getDb()->createCommand(); + $command->replace(static::indexName(), $values); + // We do not check the return value of replace because it's possible + // that the REPLACE statement doesn't change anything and thus returns 0. + $rows = $command->execute(); + } else { + $condition = $this->getOldPrimaryKey(true); + $lock = $this->optimisticLock(); + if ($lock !== null) { + if (!isset($values[$lock])) { + $values[$lock] = $this->$lock + 1; + } + $condition[$lock] = $this->$lock; } - $condition[$lock] = $this->$lock; - } - // We do not check the return value of updateAll() because it's possible - // that the UPDATE statement doesn't change anything and thus returns 0. - $rows = $this->updateAll($values, $condition); + // We do not check the return value of updateAll() because it's possible + // that the UPDATE statement doesn't change anything and thus returns 0. + $rows = $this->updateAll($values, $condition); - if ($lock !== null && !$rows) { - throw new StaleObjectException('The object being updated is outdated.'); + if ($lock !== null && !$rows) { + throw new StaleObjectException('The object being updated is outdated.'); + } } foreach ($values as $name => $value) {