I have 2 tables/models (don’t have any relation with each other) which have to be updated at the same time, and if either one can’t complete the update process, the other one won’t be updated….
if I try
if($this->model1->save($data))
$this->model2->save($data)
the model1 will be updated, no matter the model2 is successfully saved or not.
is this any rollback function that I undo the first save action if the second save action fails
You could explicitly begin and commit/rollback transactions, as suggested in a comment by Ross.
But CakePHP even supports this out of the box with
saveAssociated(). You can just pass data for both your models:There’s an option
atomicwhich controls the use of transactions:If validation fails for one of the models, the other one won’t be saved either.
Note that your database needs to support transactions. For MySQL, only the InnoDB storage engine has transaction support, MyISAM doesn’t. See comparison of MySQL storage engines.