KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I'm working on a PHP/MySQL app using the Yii framework. I've come across the following situation: In my VideoController , I have a actionCreate which creates a new Video and actionPrivacy which sets the privacy on the Video. The problem is that during the actionCreate the setPrivacy method of the Video model is called which currently has a transaction. I would like the creation of the Video to be in a transaction as well which leads to an error since a transaction is already active. In the comment on this answer , Bill Karwin writes So there's no need to make Domain Model classes or DAO classes manage transactions -- just do it at the Controller level and in this answer : Since you're using PHP, the scope of your transactions is at most a single request. So you should just use container-managed transactions, not service-layer transa. That is, start the transaction at the start of handling the request, and commit (or rollback) as you finish handling the request. If I manage the transactions in the controller, I would have a bunch of code that looks like: public function actionCreate() { $trans = Yii::app()->getDb()->beginTransaction(); ...action code... $trans->commit(); } That leads to duplicated code in a lot of places where I need transactions for the action. Or I could refactor it into the beforeAction() and afterAction() methods of the parent Controller class which would then automatically create transactions for each action being performed. Would there be any problems with this method? What is a good practice for transaction management for a PHP a
Tags (comma-separated)
Save Edits
Cancel