Reputation: 1081
Is there any way to run a model command such as $this->MyModel->saveall($rows) but without it actually performing an action on the database, just displaying all the queries it would run, the way it does when one of the queries has an error?
Upvotes: 2
Views: 280
Reputation: 2693
Yes you can, have a look at "Transactions" http://book.cakephp.org/2.0/en/models/transactions.html
// get the datasource and store it in a local variable
$ds = $this->MyModel->getDataSource();
// begin a "transaction"
$ds->begin();
// do your saving
$this->MyModel->saveAll($rows); // you can add more queries here, that's what transactions are all about! :)
// rollback, in a normal situation you would check if the save was successful and commit()/rollbac() depending on the situation.
$ds->rollback();
Please note: Auto Increment fields WILL increment, due to the fact that MySQL or any other database engine will "reserve" these ID's while doing the transaction in order to prevent duplicated ID's. This shouldn't be of any concern, but when you are debugging and you are remembering an ID , it could give you a headache if it's Monday morning (been there, done that)... ;-)
Upvotes: 2