I am using innoDB based Database. How I can use the Transaction to save the data.
I have an array of users. But only saves the last record in the database. I can not use saveAll, because I have to save the data in multiple tables and all of them are interrelated.
I even can not define the model association-ship because there are huge number of entries in my database. So each time join will perform.
Please suggest how I can use the Transaction concept?
All transactions are performed on the current DataSource object.
Firstly you need to get it (in the model):
This method returns the DataSource object that is currently bound to the Model.
Then you can use the DataSource Object’s methods to begin, commit or rollback transactions:
You will have to implement your own logic of course. Since Cake 2.2 you can also do real nested transactions. A “nested transaction” according to Wikipedia is:
Of course all this is dependent on the Database you’re using. MySQL InnoDB transactional storage engine does support transactions.
And another clarification in response to this comment:
$this->Model->saveAll();does NOT need to be run in a loop! It will manage things for you “automagically”. Take it out of the loop.Furthermore your question was concerning transactions, not saving data with saveAll(). Model::saveAll() is just a wrapper for saveMany() and saveAssociated() – it will choose which method to use depending on the type of Model association.
Read the Cake Book it is all very well explained there…