sphinx\ActiveRecord update()

update() public method

Saves the changes to this active record into the associated Sphinx index.

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 \yii\sphinx\changedAttributes will be saved into database.

For example, to update an article record:

$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:

if ($this->update() !== false) {
    // update successful
} else {
    // update failed
}
public integer|boolean update ( $runValidation = true, $attributeNames = null )
$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.

return integer|boolean

The number of rows affected, or false if validation fails or beforeSave() stops the updating process.

throws yii\db\StaleObjectException

if optimistic locking is enabled and the data being updated is outdated.

throws Exception

in case update failed.

doc_Yii
2016-10-30 17:11:41
Comments
Leave a Comment

Please login to continue.