PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, January 27, 2022

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

 January 27, 2022     yii     No comments   

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---
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

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

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing