So is it possible to execute commit in the controller? I can do this only in the model.
Yes, you can commit or roll back from the controller. First you need to get a data source from one of your models. In the controller code, just refer to one of the models that you use (provided that they are all in the same database):
$ds = $this->MyModelName->getdatasource();
Then you can start, commit and roll back to this data source from the controller.
$ds->begin(); // do stuff and save data to models if($success) { $ds->commit(); } else { $ds->rollback(); }
I really have a rollback or commit in more than one place if I perform an action and redirect or complete at some point and redirect. I will just illustrate a simple case.
Transaction processing in the controller makes the most sense to me, because the action of the controller is where the transaction boundaries really exist conceptually. The idea of a transaction naturally covers updates for several models. I do this using postgres as the back-end database with Cake 2.2 and 2.3, and it works fine here. YMMV with other db engines, although I suspect.
source share