如何在cakephp中保存多条记录3.4.12

时间:2017-08-18 10:10:22

标签: php cakephp cakephp-3.0

我正在尝试将多个记录保存到单个表中。但在保存表单数据时遇到问题。问题可能在于表单元素。请帮我解决这个问题

控制器保存方法

        $data = $this->request->data();
        $stockin = TableRegistry::get('Stockin');
        $entities= $stockin->newEntities($data);
        $stockin->saveMany($entities);

表格

echo $this->Form->input("stockin.$i.date", [ 'value' => $stockindate]); 
echo $this->Form->input("stockin.$i.product_id", [ 'value' => $prod->id]);
echo $this->Form->input("stockin.$i.quantity", ['label' => false] ); 
echo $this->Form->input("stockin.$i.harvested", ['options' => 
$harvested,'label' => false]);  
echo $this->Form->input("stockin.$i.price", [ 'label' => false]); 

后置数组值

 [
'stockin' => [
    (int) 0 => [
        'date' => '2017-08-18',
        'product_id' => '3',
        'quantity' => '1',
        'harvested' => 'k',
        'price' => '1212'
    ],
    (int) 1 => [
        'date' => '2017-08-18',
        'product_id' => '2',
        'quantity' => '2112',
        'harvested' => 'k',
        'price' => '12312'
    ],
    (int) 2 => [
        'date' => '2017-08-18',
        'product_id' => '1',
        'quantity' => '12',
        'harvested' => 'k',
        'price' => '12'
    ]
]

1 个答案:

答案 0 :(得分:3)

而不是$data,您需要提及$data['stockin']

$data       =    $this->request->data();
$stockin    =    TableRegistry::get('Stockin');
$entities   =    $stockin->newEntities($data['stockin']); // Modify this line
$stockin->saveMany($entities);