Cake Don'ts::When Not To Use Save()
31 May 2007 10:19:05
Category: CakePHP
There is a time in which $this->Model->save() is not a good idea. Here's the scenario.
Imagine you have a table with no id or primary key. It has 1 million plus rows. You need to save some data to it. So you execute $this->Model->save($data_array). What's the result? The query executes. Then Cake tries to capture the last inserted id and store it in the model as $this->id. It tries to do this with MySQL with a query: SELECT LAST_INSERT_ID FROM table; In this case there is no last insert ID so the result is an array of over 1 million zeros. The result is then looped through in Cake style. In one case today this operation consumed over 400 megabytes of memory.
So the lesson to learn? First don't make a table with out an integer id on the table. That's stupid. If you inherited the table or can't change it, use $this->execute instead.
Cheers!







