yii\sphinx\ActiveRecord::update PHP 메소드

update() 공개 메소드

This method performs the following steps in order: 1. call [[beforeValidate()]] when $runValidation is true. If validation fails, it will skip the rest of the steps; 2. call [[afterValidate()]] when $runValidation is true. 3. call [[beforeSave()]]. If the method returns false, it will skip the rest of the steps; 4. save the record into index. If this fails, it will skip the rest of the steps; 5. call [[afterSave()]]; In the above step 1, 2, 3 and 5, events [[EVENT_BEFORE_VALIDATE]], [[EVENT_BEFORE_UPDATE]], [[EVENT_AFTER_UPDATE]] and [[EVENT_AFTER_VALIDATE]] will be raised by the corresponding methods. Only the [[changedAttributes|changed attribute values]] will be saved into database. For example, to update an article record: php $article = Article::findOne($id); $article->genre_id = $genreId; $article->group_id = $groupId; $article->update(); Note that it is possible the update does not affect any row in the table. In this case, this method will return 0. For this reason, you should use the following code to check if update() is successful or not: php if ($this->update() !== false) { update successful } else { update failed }
public update ( boolean $runValidation = true, array $attributeNames = null ) : integer | false
$runValidation boolean whether to perform validation before saving the record. If the validation fails, the record will not be inserted into the database.
$attributeNames array list of attributes that need to be saved. Defaults to null, meaning all attributes that are loaded from DB will be saved.
리턴 integer | false the number of rows affected, or `false` if validation fails or [[beforeSave()]] stops the updating process.
    public function update($runValidation = true, $attributeNames = null)
    {
        if ($runValidation && !$this->validate($attributeNames)) {
            return false;
        }
        $db = static::getDb();
        if ($this->isTransactional(self::OP_UPDATE) && $db->getTransaction() === null) {
            $transaction = $db->beginTransaction();
            try {
                $result = $this->updateInternal($attributeNames);
                if ($result === false) {
                    $transaction->rollBack();
                } else {
                    $transaction->commit();
                }
            } catch (\Exception $e) {
                $transaction->rollBack();
                throw $e;
            }
        } else {
            $result = $this->updateInternal($attributeNames);
        }
        return $result;
    }