如何使用蛋糕 php 在 table 中保存多个条目

How to save multiple entries in table using cake php

如何使用 cake php 在 table 中保存多个条目?这是我到目前为止编写的代码。

$err_counter = 1;
$data3 = array();
foreach ($data2['MembershipTime'] as $k => $values) 
     {
            $batch_id = $lastid;
            $day = $values['day'];
            $skip =  (!empty($values['skip']))  ? $values['skip'] : "";
            $h1 =  (!empty($values['h1']))  ? $values['h1'] : "00";
            $m1 =  (!empty($values['m1']))  ? $values['m1'] : "00";
            $h2 =  (!empty($values['h2']))  ? $values['h2'] : "00";
            $m2 =  (!empty($values['m2']))  ? $values['m2'] : "00";

            $data3['BatchesTimes']['batch_id'] = $batch_id;
            $data3['BatchesTimes']['day'] = $day;
            $data3['BatchesTimes']['skip'] = $skip;
            $data3['BatchesTimes']['h1'] = $h1;
            $data3['BatchesTimes']['m1'] = $m1;
            $data3['BatchesTimes']['h2'] = $h2;

            $obj = $this->BatchesTimes->save($data3);
     }

这里我将使用我自己的数组示例来解释 you.You 根据数组中的名称更改变量。

$this->ModelName->saveAll($data3);

如果上述解决方案不起作用意味着尝试解决示例中提到的以下解决方案。

在你的情况下 $dataMulti = $data3; 示例:

 $dataMulti = array(
        array(
            'Info' =>
            array(
                'note' => "This is note 1",
                'delete_flag' => "f"
            )
        ),
        array(
            'Info' =>
            array(
                'note' => "This is note 2",
                'delete_flag' => "f"
            )
        ),
        array(
            'Info' =>
            array(
                'note' => "This is note 3",
                'delete_flag' => "f"
            )
        ),
    );

一次保存多条记录我们有以下两种方式:

1. $infoModel->saveAll($dataMulti);

2. $infoModel->saveMany($dataMulti);

希望有用。