【问题标题】:Update records in CakePHP (array format)CakePHP 中的更新记录(数组格式)
【发布时间】:2016-02-28 11:14:08
【问题描述】:

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

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
            )

    )

【问题讨论】:

    标签: cakephp orm cakephp-2.0 cakephp-2.x


    【解决方案1】:

    您可以像这样创建表单字段,可以将以下代码放入循环或其他替代方式中,以便您可以动态获取 $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);
    

    如果您需要更多信息,请告诉我, 如果您觉得此答案有用,请将此答案标记为可接受。

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 如果数组是固定索引的,这很容易。教师人数可以是 150,那么我如何在循环中将值设置为这种格式?你能指导我吗?
      • 只需使用array_push($record)$data[]=$record。但是,不知道为什么要一次添加 150 位教师。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多