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

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

WBOY
Release: 2016-06-06 20:25:00
Original
2430 people have browsed it

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>
Copy after login
Copy after login

为什么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>
Copy after login
Copy after login

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template