2016-02-28 106 views
-1

如何保存这种类型的数据?它给了我CakePHP更新记录(数组格式)

Array to String conversion error. 

asc201516s_teachers表有以下几列:

  • teachers_name
  • teachers_cnic
  • teachers_gender
  • teachers_contact

我的数据数组是:

[asc201516s_teachers] => Array 
    (
     [teachers_name] => Array 
      (
       [0] => asd asd 
       [1] => asd asd asd 
       [2] => asd asd asd asd 
      ) 

     [teachers_cnic] => Array 
      (
       [0] => 32312-1212121-2 
       [1] => 33434-3434343-4 
       [2] => 34454-5454545-4 
      ) 

     [teachers_gender] => Array 
      (
       [0] => 1 
       [1] => 2 
       [2] => 2 
      )    

     [teachers_contact] => Array 
      (
       [0] => 1234-5678910 
       [1] => 2345-6789101 
       [2] => 3456-7891011 
      ) 

    ) 

回答

2

你可以像这样创建你的表单域,你可以把下面的代码放在一个循环或其他替代方式中,这样你就可以获得动态值的$ key变量,例如:0,1,2,3,4。 。等

echo $this->Form->input('modelName.' . $key . '.teachers_name', array()); 
echo $this->Form->input('modelName.' . $key . '.teachers_cnic', array()); 
echo $this->Form->input('modelName.' . $key . '.teachers_gender', array()); 
echo $this->Form->input('modelName.' . $key . '.teachers_contact', array()); 

提交表单你能接受的形式在您的控制器像这样

$data=array(
    '0' => array(
     'teachers_name'=>'asd asd', 
     'teachers_cnic'=>'32312-1212121-2', 
     'teachers_gender'=>'1', 
     'teachers_contact'=>'1234-5678910' 
    ), 
    '1' => array(
     'teachers_name'=>'asd asd asd', 
     'teachers_cnic'=>'33434-3434343-4', 
     'teachers_gender'=>'2', 
     'teachers_contact'=>'2345-6789101' 
     ), 
    '2' => array(
     'teachers_name'=>'asd asd asd asd', 
     'teachers_cnic'=>'34454-5454545-4', 
     'teachers_gender'=>'2', 
     'teachers_contact'=>'3456-7891011' 
    ) 
); 

然后,你可以保存表单数据,如以下方式后。

$this->Teacher->saveMany($data); 

如果您需要了解更多信息,请让我知道, 如果你发现这个答案是非常有用的,请注明这个答案是可接受的。

0

适当的方式保存在CakePHP的2.x的多个记录如下:

$data=array(
    '0' => array(
     'teachers_name'=>'asd asd', 
     'teachers_cnic'=>'32312-1212121-2', 
     'teachers_gender'=>'1', 
     'teachers_contact'=>'1234-5678910' 
    ), 
    '1' => array(
     'teachers_name'=>'asd asd asd', 
     'teachers_cnic'=>'33434-3434343-4', 
     'teachers_gender'=>'2', 
     'teachers_contact'=>'2345-6789101' 
     ), 
    '2' => array(
     'teachers_name'=>'asd asd asd asd', 
     'teachers_cnic'=>'34454-5454545-4', 
     'teachers_gender'=>'2', 
     'teachers_contact'=>'3456-7891011' 
    ) 
); 

$this->Teacher->saveMany($data); 

+0

如果数组是固定索引,那很容易。教师数量可以是150,那么我将如何在循环中将这个值设置为这种格式?你能指导我吗? – Moorani

+0

只需使用'array_push($ record)'或'$ data [] = $ record'。然而,不知道为什么你想一次增加150名教师。 –