首先我正在使用cakePHP 2.3框架,我创建了一个表格,我也有一些关系。好的是,如果字段不是如下所示的数组形式,我能够保存数据。
<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'][]" />
但是当我提交上面的表单数据时,我收到此错误。
错误:SQLSTATE [42S22]:未找到列:1054未知列&#39;数组&#39;在&#39;字段列表&#39;
查看内置调试函数中cakePHP生成的信息。
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'
)
)
在我解决这个问题的路上,我想我可以使用for循环来迭代提交的项目,然后将它们(数据)发送到模型。不幸的是,我的for循环不能迭代直到第二行。
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);
}
提交后的示例结果。
行数:2
第0行:Kansanga小学PLE 1990 2004
答案 0 :(得分:1)
在循环中,您需要创建如下数据:
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);
}
我不确定你为什么使用$ this-&gt; Staff-&gt; Academicrecord而不是$ this-&gt; Academicrecord。
答案 1 :(得分:0)
我终于从这里得到了答案。 http://topdsoft.com/stories/view/186