Reputation: 27
First of all am using cakePHP 2.3 framework and I have created form where I also have some relationships. The good thing is that I'm able to save the data, if the fields are not in array form like below.
<input type="text" name="data['Academicrecord']['school'][]" />
<input type="text" name="data['Academicrecord']['award'][]" />
<input type="text" name="data['Academicrecord']['from'][]" />
<input type="text" name="data['Academicrecord']['to'][]" />
But I get this error when I submit the form data above.
Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Array' in 'field list'
Look at the info generated by the cakePHP in built debug function.
debug($this->request->data);
'Academicrecord' => array(
'school' => array(
(int) 0 => 'Kansanga Primary School',
(int) 1 => 'Tropical High School'
),
'award' => array(
(int) 0 => 'P.L.E',
(int) 1 => 'U.C.E'
),
'from' => array(
(int) 0 => '1997',
(int) 1 => '2003'
),
'to' => array(
(int) 0 => '2002',
(int) 1 => '2006'
)
)
On my way to solving this problem, I thought I could use the for loop to iterate the submitted items and then send them(data) to the model. Unfortunately my for loops cannot iterate the until the second row.
echo "The counts: ".count( $this->request->data['Academicrecord']['school']).'<br/>';
for($i=0;$i<count( $this->request->data['Academicrecord']['school']);$i++){
$this->request->data['Academicrecord']['staff_id'] = $this->Staff->id;
echo "{$i} :".$this->request->data['Academicrecord']['school'] = $this->request->data['Academicrecord']['school'][$i];
echo ' '.$this->request->data['Academicrecord']['award'] = $this->request->data['Academicrecord']['award'][$i];
echo ' '.$this->request->data['Academicrecord']['from'] = $this->request->data['Academicrecord']['from'][$i];
echo ' '.$this->request->data['Academicrecord']['to'] = $this->request->data['Academicrecord']['to'][$i];
echo "<br/>";
//$this->Staff->Academicrecord->save($this->request->data);
}
Sample result after submitting.
The counts of rows: 2
Row 0 :Kansanga Primary School PLE 1990 2004
Upvotes: 0
Views: 103
Reputation: 27
I finally got the answer from here. http://topdsoft.com/stories/view/186
Upvotes: 0
Reputation: 281
In loop you need to create the data like:
echo "The counts: ".count( $this->request->data['Academicrecord']['school']).'<br/>';
for($i=0;$i<count( $this->request->data['Academicrecord']['school']);$i++){
$this->request->data['Academicrecord']['staff_id'] = $this->Staff->id;
echo "{$i} :".$this->request->data['Academicrecord']['school'] = $this->request->data['Academicrecord']['school'][$i];
echo ' '.$this->request->data['Academicrecord']['award'] = $this->request->data['Academicrecord']['award'][$i];
echo ' '.$this->request->data['Academicrecord']['from'] = $this->request->data['Academicrecord']['from'][$i];
echo ' '.$this->request->data['Academicrecord']['to'] = $this->request->data['Academicrecord']['to'][$i];
//echo "<br/>";
**$this->Staff->Academicrecord->create();**
$this->Staff->Academicrecord->save($this->request->data);
}
And i am not sure why you are using $this->Staff->Academicrecord instead of $this->Academicrecord.
Upvotes: 1