Исправления

This commit is contained in:
Arsen Mirzaev Tatyano-Muradovich 2021-03-29 10:20:46 +10:00
parent a7abed56d6
commit 7a6e6fbada
2 changed files with 34 additions and 11 deletions

View File

@ -107,7 +107,7 @@ abstract class ActiveRecord extends BaseActiveRecord
{ {
/** @var ActiveQuery $query */ /** @var ActiveQuery $query */
$query = Yii::createObject(ActiveQuery::class, [get_called_class()]); $query = Yii::createObject(ActiveQuery::class, [get_called_class()]);
$query->collection(static::collectionName())->select(static::collectionName()); $query->in(static::collectionName())->select(static::collectionName());
return $query; return $query;
} }
@ -170,6 +170,7 @@ abstract class ActiveRecord extends BaseActiveRecord
if ($runValidation && !$this->validate($attributes)) { if ($runValidation && !$this->validate($attributes)) {
return false; return false;
} }
$result = $this->insertInternal($attributes, $options); $result = $this->insertInternal($attributes, $options);
return $result; return $result;
@ -189,7 +190,9 @@ abstract class ActiveRecord extends BaseActiveRecord
} }
$newId = static::getDb()->getDocumentHandler()->save(static::collectionName(), $values); $newId = static::getDb()->getDocumentHandler()->save(static::collectionName(), $values);
static::populateRecord($this, static::getDb()->getDocument(static::collectionName(), $newId)); static::populateRecord($this, static::getDb()->getDocument(static::collectionName(), $newId));
$this->setIsNewRecord(false); $this->setIsNewRecord(false);
$changedAttributes = array_fill_keys(array_keys($values), null); $changedAttributes = array_fill_keys(array_keys($values), null);
@ -217,8 +220,11 @@ abstract class ActiveRecord extends BaseActiveRecord
$this->afterSave(false, $values); $this->afterSave(false, $values);
return 0; return 0;
} }
$condition = $this->getOldPrimaryKey(true); $condition = $this->getOldPrimaryKey(true);
$lock = $this->optimisticLock(); $lock = $this->optimisticLock();
if ($lock !== null) { if ($lock !== null) {
if (!isset($values[$lock])) { if (!isset($values[$lock])) {
$values[$lock] = $this->$lock + 1; $values[$lock] = $this->$lock + 1;
@ -230,12 +236,17 @@ abstract class ActiveRecord extends BaseActiveRecord
$this->setAttribute($key, $attribute); $this->setAttribute($key, $attribute);
} }
$rows = (new Query())->options($options)->update( $rows = (new Query())
static::collectionName(), ->options($options)
->in(static::collectionName())
->where(['_key' => $this->getOldAttribute('_key')])
->update(
array_merge(
$values, $values,
[ [
'_key' => $this->getOldAttribute('_key'), '_key' => $this->getOldAttribute('_key'),
] ]
)
); );
if ($lock !== null && !$rows) { if ($lock !== null && !$rows) {
@ -298,7 +309,7 @@ abstract class ActiveRecord extends BaseActiveRecord
*/ */
public static function updateAll($attributes, $condition = [], $options = []) 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 = []) 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() public static function truncate()
@ -369,6 +380,7 @@ abstract class ActiveRecord extends BaseActiveRecord
public function delete($options = []) public function delete($options = [])
{ {
$result = false; $result = false;
if ($this->beforeDelete()) { if ($this->beforeDelete()) {
$result = $this->deleteInternal($options); $result = $this->deleteInternal($options);
$this->afterDelete(); $this->afterDelete();
@ -384,14 +396,23 @@ abstract class ActiveRecord extends BaseActiveRecord
protected function deleteInternal($options = []) protected function deleteInternal($options = [])
{ {
$condition = $this->getOldPrimaryKey(true); $condition = $this->getOldPrimaryKey(true);
$lock = $this->optimisticLock(); $lock = $this->optimisticLock();
if ($lock !== null) { if ($lock !== null) {
$condition[$lock] = $this->$lock; $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) { if ($lock !== null && !$result) {
throw new StaleObjectException('The object being deleted is outdated.'); throw new StaleObjectException('The object being deleted is outdated.');
} }
$this->setOldAttributes(null); $this->setOldAttributes(null);
return $result; return $result;

View File

@ -896,6 +896,7 @@ class Query extends Component implements QueryInterface
$this->limit(1); $this->limit(1);
$statement = $this->createCommand($db); $statement = $this->createCommand($db);
$token = $this->getRawAql($statement); $token = $this->getRawAql($statement);
Yii::info($token, 'mirzaev\yii2\arangodb\Query::query'); Yii::info($token, 'mirzaev\yii2\arangodb\Query::query');
try { try {
Yii::beginProfile($token, 'mirzaev\yii2\arangodb\Query::query'); Yii::beginProfile($token, 'mirzaev\yii2\arangodb\Query::query');
@ -942,6 +943,7 @@ class Query extends Component implements QueryInterface
$statement = $this->getStatement($params, $db); $statement = $this->getStatement($params, $db);
$token = $this->getRawAql($statement); $token = $this->getRawAql($statement);
Yii::info($token, 'mirzaev\yii2\arangodb\Query::insert'); Yii::info($token, 'mirzaev\yii2\arangodb\Query::insert');
try { try {
Yii::beginProfile($token, 'mirzaev\yii2\arangodb\Query::insert'); Yii::beginProfile($token, 'mirzaev\yii2\arangodb\Query::insert');