user936565
user936565

Reputation: 175

How to save multiple records in cakephp 3.4.12

i am trying to save multiple records in to single table. But facing problem while saving the form data. The problem might be with the form elements. please help me on this issue

Controller save method

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

Form

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

Post array value is

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

]

Upvotes: 0

Views: 558

Answers (1)

Indrasis Datta
Indrasis Datta

Reputation: 8608

Instead of $data, you need to mention $data['stockin']

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

Upvotes: 3

Related Questions