CakePHP: Multiple Inserts - Insert multiple rows using single statement.

Using the method saveAll(), we can achieve the desired goal. http://book.cakephp.org/view/75/Saving-Your-Data

Example:
If you want to save multiple records/rows into the same table.


$multipleRowData = Array(   
 Array('name' => 'Bob', 'phone' => "123-123-7777"),  
 Array('name' => 'Ann', 'phone' => "987-123-5555"),  
 Array('name' => 'Tom', 'phone' => "788-123-4569")  
 );   
 $this->Mymodelname->saveAll($multipleRowData);  


This would result in inserting 3 rows in the "mymodelname" table.

2 comments:

  1. if u set debug = 2 in core.php and look over the queries you see it insert the records multiple "insert" queries where this is wrong it should be in one single insert query like:
    "insert into table (field1, field2) values('row1','row1'),('row2',row2')"

    ReplyDelete
  2. works great thanks

    ReplyDelete

Python contextlib for Timing Python code

If you've ever found yourself needing to measure the execution time of specific portions of your Python code, the `contextlib` module o...