protected function updateInternal($attributes = null)
{
if (!$this->beforeSave(false)) {
return false;
}
$values = $this->getDirtyAttributes($attributes);
if (empty($values)) {
$this->afterSave(false, $values);
return 0;
}
$useReplace = false;
$indexSchema = $this->getIndexSchema();
if ($this->getIndexSchema()->isRt) {
foreach ($values as $name => $value) {
$columnSchema = $indexSchema->getColumn($name);
if ($columnSchema->isField) {
$useReplace = true;
break;
}
}
}
if ($useReplace) {
$values = array_merge(
$this->getAttributes(),
$values,
$this->getOldPrimaryKey(true)
);
$command = static::getDb()->createCommand();
$command->replace(static::indexName(), $values);
$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;
}
$rows = $this->updateAll($values, $condition);
if ($lock !== null && !$rows) {
throw new StaleObjectException('The object being updated is outdated.');
}
if (isset($values[$lock])) {
$this->$lock = $values[$lock];
}
}
$changedAttributes = [];
foreach ($values as $name => $value) {
$changedAttributes[$name] = $this->getOldAttribute($name);
$this->setOldAttribute($name, $value);
}
$this->afterSave(false, $changedAttributes);
return $rows;
}