php - insert multiple rows in a saveall in cakephp -


i'm newbie in cake , wodering how insert multiple rows in single saveall function, got table,

create table if not exists `dates` ( `date` varchar(10) collate utf8_unicode_ci not null ) 

what i'm trying let user select start date , end date using jquery calander, once submit dates between range saved database, got 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', ) 

` controller looks 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  $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 can't foreach work, if uncomment foreach, got error said illegal string offset 'dates' , commented , try add start date , end date array see if works, got error said.

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

) ` notice (8): array string conversion [core\cake\model\datasource\dbosource.php, line 1005]code

cuz i'm trying insert 2 values 1 field...i know should sth like

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

`but can't figure out how it. appreciate!!!!

the structure you'll want array save multiple dates using saveall() this:

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

Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -