yii\sphinx\ActiveRecord::updateInternal PHP Méthode

updateInternal() protected méthode

See also: CActiveRecord::update()
protected updateInternal ( $attributes = null )
    protected function updateInternal($attributes = null)
    {
        if (!$this->beforeSave(false)) {
            return false;
        }
        $values = $this->getDirtyAttributes($attributes);
        if (empty($values)) {
            $this->afterSave(false, $values);
            return 0;
        }
        // Replace is supported only by runtime indexes and necessary only for field update
        $useReplace = false;
        $indexSchema = $this->getIndexSchema();
        if ($this->getIndexSchema()->isRuntime) {
            foreach ($values as $name => $value) {
                $columnSchema = $indexSchema->getColumn($name);
                if ($columnSchema->isField) {
                    $useReplace = true;
                    break;
                }
            }
        }
        if ($useReplace) {
            $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};
            }
            // 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 (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;
    }