以CakePHP(数组格式)更新记录

以CakePHP(数组格式)更新记录,cakephp,orm,cakephp-2.0,cakephp-2.x,Cakephp,Orm,Cakephp 2.0,Cakephp 2.x,如何保存此类数据?它给了我 Array to String conversion error. Myasc201516s_教师表包含以下列: teachers\u name 教师\u cnic teachers\u性别 教师\u联系方式 我的数据数组是: [asc201516s_teachers] => Array ( [teachers_name] => Array ( [0] => a

如何保存此类数据?它给了我

Array to String conversion error. 
My
asc201516s_教师
表包含以下列:

  • teachers\u name
  • 教师\u cnic
  • teachers\u性别
  • 教师\u联系方式
我的数据数组是:

 [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
            )

    )

在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);


您可以创建这样的表单字段,您可以将下面的代码放入循环或其他替代方式中,以便动态获取$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);
如果您需要更多信息,请告诉我,
如果您发现此答案有用,请将此答案标记为可接受。

如果数组具有固定索引,则很容易。教师计数可以是150,那么我如何在循环中设置此格式的值呢?你能指导我吗?只需使用
array\u push($record)
$data[]=$record
。然而,我不知道你为什么要一次增加150名教师。