-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
"nested transactions" in pomm? #8
Comments
Each Public method in a ModelLayer is a transaction. If several methods want to share some code, they call a protected (or private) method that assumes it is in a transaction block. It can throw an exception in case of a failure which is caught by the public method managing the transaction. If you want a partial rollback, use the savepoints. |
This would only be possible if all methods were inside the I've already found a solution using a transaction trait which overwrites the implementation, when I need to. But to be more clear about the problem and because I am interested how this would be solved with pomm, I give an example of what I need to do: Supposed, I have different types of hierarchical data objects, let's say categories and menu items. Both have nothing to do with each other. Instead of using parent-child relations, I often use nested sets for hierarchical data, which does not often change. Therefor I have three tables: In pomm this could lead to three $categoriesModel->createAsChildOf($rootCategory);
-> begin transaction
-> $newNode = $nestedNodesModel->createNodeAsChildOf( $rootCategory->node_id );
-> savepoint x;
-> $nestedNodesModel->spreadForChildRightInsert(); // make space for a new node
-> $nestedNodesModel->createAndSave(['lft' => ..., 'rgt' => ...]);
-> release savepoint x;
-> $newCategory = $categoriesModel->createAndSave(['node_id' => $newNode->id, ...]);
-> commit transaction As you can see the code is distributed over different models using transactions. Nested nodes could also be created without any other objects depending on it. So actually they need to know if they should use If I would now add a test which also wraps the whole thing into a But, there is one more problem: pomm does not "allow" to use transactions in I hope, I could make the above problem more clear. In my experience it is not an uncommon case to use "nested transactions". If you are interested, I've added a rough version of TransactionTrait here. This let me even insert transaction methods into |
The fact that transaction methods are only available in Model layer is here to group model calls in transactions and to turn technical exceptions into busines oriented exceptions. Several model classes can be used in the same transaction within the same Organizing the code between the model and model layer classes is as important as between controllers and model. In the |
Thank you for making this clear. Wouldn't it be good then, to also allow access to other In my example above it would help when |
Ok, I understand why you get confused. The name of the model layer class is not correlated with model classes it uses nor related tables. Since database schemas may be seen as functionnal grouping of elements, maybe a good model layer class name would be the name of the schema like |
You are right, that's what I thought :-) Wouldn't you agree, that e.g. a But I think I'll find my way around this. I really don't want to waste your time any longer :-) Thanks |
Can I close this issue now ? |
Yes, thanks. |
As I understand the ModelLayer concept, this is the place to put multiple queries into one transaction.
Supposed I have a method in a
ModelLayer
class which uses a transaction and want to wrap that method call into another transaction. InModelLayer
I have to check if there is currently an active transaction and decide whether to usestartTransaction()
orsetSavepoint()
. This goes like this (every time)Same goes for commits and rollbacks which is pretty annoying. Is there already an elegant way to avoid this?
I usually solved this by implementing a simple transaction stack, who keeps track of ongoing transactions and save points. begin, commit or rollback methods take care of what method to use. I've copied a trivial example in here.
The text was updated successfully, but these errors were encountered: