Home >Backend Development >PHP Tutorial >Yii2 框架自带的ActiveRecord 事务嵌套分析

Yii2 框架自带的ActiveRecord 事务嵌套分析

WBOY
WBOYOriginal
2016-06-06 20:25:002424browse

ActiveRecord 这个class文件update方法里面如何下:

public function update($runValidation = true, $attributeNames = null)

<code>{
    if ($runValidation && !$this->validate($attributeNames)) {
        Yii::info('Model not updated due to validation error.', __METHOD__);
        return false;
    }

    if (!$this->isTransactional(self::OP_UPDATE)) {
        return $this->updateInternal($attributeNames);
    }

    $transaction = static::getDb()->beginTransaction();
    try {
        $result = $this->updateInternal($attributeNames);
        if ($result === false) {
            $transaction->rollBack();
        } else {
            $transaction->commit();
        }
        return $result;
    } catch (\Exception $e) {
        $transaction->rollBack();
        throw $e;
    }
}


</code>

为什么update底层也用了事务操作,如果在外面也开启事务,事务嵌套,会造成什么影响?

回复内容:

ActiveRecord 这个class文件update方法里面如何下:

public function update($runValidation = true, $attributeNames = null)

<code>{
    if ($runValidation && !$this->validate($attributeNames)) {
        Yii::info('Model not updated due to validation error.', __METHOD__);
        return false;
    }

    if (!$this->isTransactional(self::OP_UPDATE)) {
        return $this->updateInternal($attributeNames);
    }

    $transaction = static::getDb()->beginTransaction();
    try {
        $result = $this->updateInternal($attributeNames);
        if ($result === false) {
            $transaction->rollBack();
        } else {
            $transaction->commit();
        }
        return $result;
    } catch (\Exception $e) {
        $transaction->rollBack();
        throw $e;
    }
}


</code>

为什么update底层也用了事务操作,如果在外面也开启事务,事务嵌套,会造成什么影响?

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn