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;
}
}