'insert multiple rows in a saveall in cakephp

i'm newbie in Cake and wodering how to insert multiple rows in a single saveall function, i got this table,

CREATE TABLE IF NOT EXISTS `dates` (
`date` varchar(10) COLLATE utf8_unicode_ci NOT NULL
)

what i'm trying to do is let user select start date and end date using JQuery calander, once submit all the dates between this range will be saved into database, i already got the array of dates eg:

`array(
      (int) 0 => '5/8/2013',
(int) 1 => '6/8/2013',
(int) 2 => '7/8/2013',
(int) 3 => '8/8/2013',
)

` then my controller looks like this:

public function index(){

if ($this->request->is('post')) {
                 $this->Date->create();

            $data = array();
            $data['dates']=array();

            $startDate = $this->request->data['Date']['from'];
            $endDate = $this->request->data['Date']['to'];

            $datesBlocked = $this->loopDates($this->request->data['Date']['from'],$this->request->data['Date']['to']);

            $data['dates'][] = $this->request->data['Blockdate']['from'];
            $data['dates'][] = $this->request->data['Blockdate']['to'];

            /*foreach($datesBlocked  as $data) {             
            $data['dates'][] = $data;               
            }*/

            if($this->Date->saveAll($data)) {

                $this->Session->setFlash(__('done'));

                 if ($this->Session->read('UserAuth.User.user_group_id') == 1) {
                   // $this->redirect("/manages");
                }
            }
            }
public function loopDates($from,$to){
         $blockdates = array();     
    $start = strtotime($from); 
    $end = strtotime($to);  
    debug($start);
    $counter = 0;       
 for($t=$start;$t<=$end;$t+=86400) {
            $d = getdate($t);
            $blockdates[$counter++] =  $d['mday'].'/'.$d['mon'].'/'.$d['year'];
}   
debug($blockdates);
return $blockdates; 

}

issue was i can't get foreach work, if i uncomment the foreach, i got error said Illegal string offset 'dates' , so i commented that and try to only add the start date and end date to the array to see if that works, then i got another error said.

`array(
'dates' => array(
    (int) 0 => '08/05/2013',
    (int) 1 => '09/05/2013'
)

) ` Notice (8): Array to string conversion [CORE\Cake\Model\Datasource\DboSource.php, line 1005]Code

cuz i'm trying to insert 2 values into one field...i know it should be sth like

`array(
  'dates' => array( (int) 0 => '08/05/2013',        
)
  'dates' => array((int) 1 => '09/05/2013'
      ))

`but can't figure out how to do it. Any help would be much appreciate!!!!



Solution 1:[1]

The structure you'll want your array to save multiple dates using saveAll() is this:

array(
    'Date' => array(
        0 => array(
            'date' => '08/05/2013',
        ),
        1 => array(
            'date' => '09/05/2013',
        )
    ),
)

Solution 2:[2]

I know that this is a little late, but to write multiple rows in a loop, you have to proceed the save with a create().

eg:

foreach($items as $lineItem){

    $this->Invoice->create();

    $this->Invoice->save(array(
        'user_id'=>$property['User']['id'],
        'invoice_id'=>$invId['Invoices']['id'],
        'item_id'=>$lineItem['item_number'],    
        'quantity'=>$lineItem['quantity'],
        'price'=>$lineItem['mc_gross']
    );

}

Just thought it was worth mentioning, hopefully it will help someone.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Deano