You are here: cakephp » solution to the topic submitting an array into a database for multiple entries
Solution to the topic "Submitting an array into a database for multiple entries
- Written By
- Shadow
- Submitted At
- 2011-03-17 08:47:18
- Num Views
- 539
- Category
- CakePHP
|
Ok, so the problem I was facing a while ago was writing an array into a database for more than one entry. The solution was actually quite simple. The trick is arranging the array into the correct structure. The array should be arranged like this when you debug($this->data); in your controller: app\controllers\points_controller.php (line 32) Array ( [Point] => Array //ModelName ( [0] => Array ( [placement] => 5 //FieldNames [breeder] => Bontstaan [owner] => Jannie [horse_name] => Florista [horse_id] => 444 [type_id] => 26 ) [1] => Array ( [placement] => 4 [breeder] => Barend [owner] => Micheal [horse_name] => Martha [horse_id] => 1123 [type_id] => 26 ) [2] => Array ( [placement] => 3 [breeder] => Bontstaan [owner] => Bontstaan [horse_name] => Bandera [horse_id] => 1 [type_id] => 26 ) ) ) This can be done with a for loop or a $count variable as I did. In your view: $count = 0; foreach($entries as $entry){ echo $form->input('Point.'.$count.'.horse_name', //'ModelName.'.$count.'FieldName' array('value'=> $entry['Entries']['horse_name'])), $form->input('Point.'.$count.'.horse_id', array('value'=> $entry['Entries']['horse_number'])), $form->input('Point.'.$count.'.type_id', array('type'=>'hidden','value'=> $types['EntriesType']['type_id'])); $count = $count + 1; } And the controller: (remember to use cake's saveAll function) $this->loadModel('Point'); if(!empty($this->data)){ if($this->Point->saveAll($this->data['Point'])){ $this->Session->setFlash('The points were updated'); $this->redirect('../points/index'); }else{ $this->Session->setFlash('There was a problem saving this information'); } } Hope this helps! By Shadow @ 2011-03-17 08:47:18
|
