Add multiple record in add controller in cakephp 3

59 Views Asked by At

I have to same multiple row in add form(controller) but it's not saving multiple record. Do i have to move save under the foreach? or what I can do to save multiple row?

$boldNumberRequest = $this->BoldNumberRequest->newEntity();                                                          
$boldNumberRequest = $this->BoldNumberRequest->patchEntity($boldNumberRequest, $this->request->data);
$this->BoldNumberRequest->saveMany($boldNumberRequest)

Here what i am getting in $this->request->data

(int) 0 => [
    'bold_number_from' => '1',
    'bold_number_to' => '1000',
    'is_approved' => '1',
    'unique_series' => '030318',
    'owner_company_id' => '1'
],
(int) 1 => [
    'bold_number_from' => '1',
    'bold_number_to' => '1000',
    'is_approved' => '1',
    'unique_series' => '100318',
    'owner_company_id' => '1'
],
1

There are 1 best solutions below

0
Bread Breeder On

Your problem is that you're creating a single entity using $this->BoldNumberRequest->newEntity() and then trying to patch it with the data of multiple entities.

What you want can be found in the CakePHP documentation about Saving Multiple Entities:

You need to create multiple entities from your data:

$entities = $this->BoldNumberRequest->newEntities($this->request->data);
$this->BoldNumberRequest->saveMany($entities);