elasticsearch\ActiveRecord update()

update() public method

Saves the changes to this active record into the associated database table.

This method performs the following steps in order:

  1. call beforeValidate() when $runValidation is true. If beforeValidate() returns false, the rest of the steps will be skipped;
  2. call afterValidate() when $runValidation is true. If validation failed, the rest of the steps will be skipped;
  3. call beforeSave(). If beforeSave() returns false, the rest of the steps will be skipped;
  4. save the record into database. 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_AFTER_VALIDATE, EVENT_BEFORE_UPDATE, and EVENT_AFTER_UPDATE will be raised by the corresponding methods.

Only the changed attribute values will be saved into database.

For example, to update a customer record:

$customer = Customer::findOne($id);
$customer->name = $name;
$customer->email = $email;
$customer->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 ($customer->update() !== false) {
    // update successful
} else {
    // update failed
}
public integer|boolean update ( $runValidation = true, $attributeNames = null, $options = [] )
$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 attribute names that need to be saved. Defaults to null, meaning all attributes that are loaded from DB will be saved.

$options array

Options given in this parameter are passed to elasticsearch as request URI parameters. These are among others:

  • routing define shard placement of this record.
  • parent by giving the primaryKey of another record this defines a parent-child relation
  • timeout timeout waiting for a shard to become available.
  • replication the replication type for the delete/index operation (sync or async).
  • consistency the write consistency of the index/delete operation.
  • refresh refresh the relevant primary and replica shards (not the whole index) immediately after the operation occurs, so that the updated document appears in search results immediately.
  • detect_noop this parameter will become part of the request body and will prevent the index from getting updated when nothing has changed.

Please refer to the elasticsearch documentation for more details on these options.

The following parameters are Yii specific:

  • optimistic_locking set this to true to enable optimistic locking, avoid updating when the record has changed since it has been loaded from the database. Yii will set the version parameter to the value stored in \yii\elasticsearch\version. See the elasticsearch documentation for details.

    Make sure the record has been fetched with a \yii\elasticsearch\version before. This is only the case for records fetched via get() and mget() by default. For normal queries, the _version field has to be fetched explicitly.

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 yii\base\InvalidParamException

if no \yii\elasticsearch\version is available and optimistic locking is enabled.

throws yii\elasticsearch\Exception

in case update failed.

doc_Yii
2016-10-30 17:01:28
Comments
Leave a Comment

Please login to continue.