diff --git a/mirzaev/yii2/arangodb/ActiveRecord.php b/mirzaev/yii2/arangodb/ActiveRecord.php index ef05456..3f475e9 100644 --- a/mirzaev/yii2/arangodb/ActiveRecord.php +++ b/mirzaev/yii2/arangodb/ActiveRecord.php @@ -107,7 +107,7 @@ abstract class ActiveRecord extends BaseActiveRecord { /** @var ActiveQuery $query */ $query = Yii::createObject(ActiveQuery::class, [get_called_class()]); - $query->collection(static::collectionName())->select(static::collectionName()); + $query->in(static::collectionName())->select(static::collectionName()); return $query; } @@ -170,6 +170,7 @@ abstract class ActiveRecord extends BaseActiveRecord if ($runValidation && !$this->validate($attributes)) { return false; } + $result = $this->insertInternal($attributes, $options); return $result; @@ -189,7 +190,9 @@ abstract class ActiveRecord extends BaseActiveRecord } $newId = static::getDb()->getDocumentHandler()->save(static::collectionName(), $values); + static::populateRecord($this, static::getDb()->getDocument(static::collectionName(), $newId)); + $this->setIsNewRecord(false); $changedAttributes = array_fill_keys(array_keys($values), null); @@ -217,8 +220,11 @@ abstract class ActiveRecord extends BaseActiveRecord $this->afterSave(false, $values); return 0; } + $condition = $this->getOldPrimaryKey(true); + $lock = $this->optimisticLock(); + if ($lock !== null) { if (!isset($values[$lock])) { $values[$lock] = $this->$lock + 1; @@ -230,13 +236,18 @@ abstract class ActiveRecord extends BaseActiveRecord $this->setAttribute($key, $attribute); } - $rows = (new Query())->options($options)->update( - static::collectionName(), - $values, - [ - '_key' => $this->getOldAttribute('_key'), - ] - ); + $rows = (new Query()) + ->options($options) + ->in(static::collectionName()) + ->where(['_key' => $this->getOldAttribute('_key')]) + ->update( + array_merge( + $values, + [ + '_key' => $this->getOldAttribute('_key'), + ] + ) + ); if ($lock !== null && !$rows) { throw new StaleObjectException('The object being updated is outdated.'); @@ -298,7 +309,7 @@ abstract class ActiveRecord extends BaseActiveRecord */ public static function updateAll($attributes, $condition = [], $options = []) { - return (new Query())->options($options)->update(static::collectionName(), $attributes, $condition); + return (new Query())->options($options)->in(static::collectionName())->update($attributes, $condition); } /** @@ -319,7 +330,7 @@ abstract class ActiveRecord extends BaseActiveRecord */ public static function deleteAll($condition = [], $options = []) { - return (new Query())->options($options)->remove(static::collectionName(), $condition); + return (new Query())->options($options)->in(static::collectionName())->remove($condition); } public static function truncate() @@ -369,6 +380,7 @@ abstract class ActiveRecord extends BaseActiveRecord public function delete($options = []) { $result = false; + if ($this->beforeDelete()) { $result = $this->deleteInternal($options); $this->afterDelete(); @@ -384,14 +396,23 @@ abstract class ActiveRecord extends BaseActiveRecord protected function deleteInternal($options = []) { $condition = $this->getOldPrimaryKey(true); + $lock = $this->optimisticLock(); + if ($lock !== null) { $condition[$lock] = $this->$lock; } - $result = (new Query())->options($options)->collection(static::collectionName())->remove($condition); + + $result = (new Query()) + ->options($options) + ->in(static::collectionName()) + ->where($condition) + ->remove(); + if ($lock !== null && !$result) { throw new StaleObjectException('The object being deleted is outdated.'); } + $this->setOldAttributes(null); return $result; diff --git a/mirzaev/yii2/arangodb/Query.php b/mirzaev/yii2/arangodb/Query.php index 38c5a2f..5ef1a1b 100644 --- a/mirzaev/yii2/arangodb/Query.php +++ b/mirzaev/yii2/arangodb/Query.php @@ -896,6 +896,7 @@ class Query extends Component implements QueryInterface $this->limit(1); $statement = $this->createCommand($db); $token = $this->getRawAql($statement); + Yii::info($token, 'mirzaev\yii2\arangodb\Query::query'); try { Yii::beginProfile($token, 'mirzaev\yii2\arangodb\Query::query'); @@ -942,6 +943,7 @@ class Query extends Component implements QueryInterface $statement = $this->getStatement($params, $db); $token = $this->getRawAql($statement); + Yii::info($token, 'mirzaev\yii2\arangodb\Query::insert'); try { Yii::beginProfile($token, 'mirzaev\yii2\arangodb\Query::insert');