【问题标题】:How to save multiple records in cakephp 3.4.12如何在 cakephp 3.4.12 中保存多条记录
【发布时间】:2023-04-10 21:06:01
【问题描述】:

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

控制器保存方法

        $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'
    ]
]

]

【问题讨论】:

    标签: php cakephp cakephp-3.0


    【解决方案1】:

    你需要提到$data['stockin']而不是$data

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-29
      • 2013-10-05
      相关资源
      最近更新 更多