Thursday, January 27, 2022

[FIXED] Handling transaction when updates using ActiveRecord and DAO take place together

Issue

I know that when we adopt Active Record updates during a transaction, we should use

$trans = $model->dbConnection->beginTransaction();

And when we adopt DAO updates during a transaction, we should use

$trans = Yii::app()->db->beginTransaction();

But how to begin a transaction when we do updates via both ActiveRecord and DAO within a transaction?

So far, I have been using

$trans = Yii::app()->db->beginTransaction();

for the mixed up case.

Can anyone guide me here?


Solution

Usually a models dbConnection is Yii::app()->db. Therefore you can use Yii::app()->db->beginTransaction(); and that should work both for active record and DAO. It wont work if you have different connection in active record and DAO. But this is the normal behavior and has nothing to do with yii, because a transaction works always on the connection.

You can for e.g. use two DAOs with different connections, but you cannot use transactions for these both since they have different connections.



Answered By - chris---

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.