Commit cd3950aa by Paul Klimov

Sphinx Active Record saving advanced to use 'replace' for runtime indexes

parent 102f3868
...@@ -850,20 +850,30 @@ class ActiveRecord extends Model ...@@ -850,20 +850,30 @@ class ActiveRecord extends Model
$this->afterSave(false); $this->afterSave(false);
return 0; return 0;
} }
$condition = $this->getOldPrimaryKey(true);
$lock = $this->optimisticLock(); if ($this->getIndexSchema()->isRuntime) {
if ($lock !== null) { $values = array_merge($values, $this->getOldPrimaryKey(true));
if (!isset($values[$lock])) { $command = static::getDb()->createCommand();
$values[$lock] = $this->$lock + 1; $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.
// We do not check the return value of updateAll() because it's possible $rows = $this->updateAll($values, $condition);
// that the UPDATE statement doesn't change anything and thus returns 0.
$rows = $this->updateAll($values, $condition);
if ($lock !== null && !$rows) { if ($lock !== null && !$rows) {
throw new StaleObjectException('The object being updated is outdated.'); throw new StaleObjectException('The object being updated is outdated.');
}
} }
foreach ($values as $name => $value) { foreach ($values as $name => $value) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment