yii\elasticsearch\ActiveRecord::delete PHP Method

delete() public method

public delete ( array $options = [] ) : integer | boolean
$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. Please refer to the [elasticsearch documentation](http://www.elastic.co/guide/en/elasticsearch/reference/current/docs-delete.html) 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 [[version]]. See the [elasticsearch documentation](http://www.elastic.co/guide/en/elasticsearch/reference/current/docs-delete.html#delete-versioning) for details. Make sure the record has been fetched with a [[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 deleted, or false if the deletion is unsuccessful for some reason. Note that it is possible the number of rows deleted is 0, even though the deletion execution is successful.
    public function delete($options = [])
    {
        if (!$this->beforeDelete()) {
            return false;
        }
        if (isset($options['optimistic_locking']) && $options['optimistic_locking']) {
            if ($this->_version === null) {
                throw new InvalidParamException('Unable to use optimistic locking on a record that has no version set. Refer to the docs of ActiveRecord::delete() for details.');
            }
            $options['version'] = $this->_version;
            unset($options['optimistic_locking']);
        }
        try {
            $result = static::getDb()->createCommand()->delete(static::index(), static::type(), $this->getOldPrimaryKey(false), $options);
        } catch (Exception $e) {
            // HTTP 409 is the response in case of failed optimistic locking
            // http://www.elastic.co/guide/en/elasticsearch/guide/current/optimistic-concurrency-control.html
            if (isset($e->errorInfo['responseCode']) && $e->errorInfo['responseCode'] == 409) {
                throw new StaleObjectException('The object being deleted is outdated.', $e->errorInfo, $e->getCode(), $e);
            }
            throw $e;
        }
        $this->setOldAttributes(null);
        $this->afterDelete();
        if ($result === false) {
            return 0;
        } else {
            return 1;
        }
    }