Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cakephp 3.x saving multiple entities - newEntities

I'm having the hardest time with saving multiple records. I've tried a million things, but I end up with the same problem: my records are not saved and I can't see any errors. Bear in mind that I'm new to cakephp and a novice coder.

Am I missing something obvious and crucial?

Table:

    $this->table('splits');
    $this->displayField('id');
    $this->primaryKey('id');

    $this->belongsTo('Transactions', [
        'foreignKey' => 'transaction_id',
        'joinType' => 'INNER'
    ]);
    $this->belongsTo('Accounts', [
        'foreignKey' => 'account_credit_id',
        'joinType' => 'INNER'
    ]);

Controller:

    $splits = $this->Splits->newEntity();
    if ($this->request->is('post')) {
        $splits = $this->Splits->newEntities($this->request->data());

        debug($splits);

        foreach ($splits as $split){
            $this->Splits->save($split);
        }
   }

    $transactions = $this->Splits->Transactions->find('list', ['limit' => 200]);
    $accounts = $this->Splits->Accounts->find('list', ['limit' => 200]);
    $this->set(compact('split', 'transactions', 'accounts'));
    $this->set('_serialize', ['split']);

Template:

        echo $this->Form->input('Splits.1.transaction_id', ['options' => $transactions]);
        echo $this->Form->input('Splits.1.amount',  ['type' => 'float']);
        echo $this->Form->input('Splits.1.account_id', ['options' => $accounts]);
        echo $this->Form->input('Splits.2.transaction_id', ['options' => $transactions]);
        echo $this->Form->input('Splits.2.amount',  ['type' => 'float']);
        echo $this->Form->input('Splits.2.account_id', ['options' => $accounts]);
        echo $this->Form->input('Splits.3.transaction_id', ['options' => $transactions]);
        echo $this->Form->input('Splits.3.amount',  ['type' => 'float']);
        echo $this->Form->input('Splits.3.account_id', ['options' => $accounts]);

Debug on $splits:

[
(int) 0 => object(App\Model\Entity\Split) {

    (int) 1 => [
        'transaction_id' => '108',
        'amount' => '100.33',
        'account_id' => '2'
    ],
    (int) 2 => [
        'transaction_id' => '108',
        'amount' => '50.22',
        'account_id' => '4'
    ],
    (int) 3 => [
        'transaction_id' => '108',
        'amount' => '65.22',
        'account_id' => '5'
    ],
    '[new]' => true,
    '[accessible]' => [
        '*' => true
    ],
    '[dirty]' => [
        (int) 1 => true,
        (int) 2 => true,
        (int) 3 => true
    ],
    '[original]' => [],
    '[virtual]' => [],
    '[errors]' => [],
    '[repository]' => 'Splits'

}
]
like image 203
cKelly Avatar asked Oct 02 '15 17:10

cKelly


2 Answers

Did you somewhere saw this Table.index.field style being used, or did you just tried something and hoped it would work?

When saving many records respectively creating many entities, the expected format is a numerically indexed array that holds the data for the individual records, just as shown in the docs

Cookbook > Database Access & ORM > Saving Data > Converting Multiple Records

When creating forms that create/update multiple records at once you can use newEntities():

[...]

In this situation, the request data for multiple articles should look like:

$data = [
    [
        'title' => 'First post',
        'published' => 1
    ],
    [
        'title' => 'Second post',
        'published' => 1
    ],
];

So your inputs should not use the table name, but just the index and the field name, like

echo $this->Form->input('0.transaction_id', /* ... */);
echo $this->Form->input('0.amount', /* ... */);
echo $this->Form->input('0.account_id', /* ... */);
echo $this->Form->input('1.transaction_id', /* ... */);
echo $this->Form->input('1.amount', /* ... */);
echo $this->Form->input('1.account_id', /* ... */);
echo $this->Form->input('2.transaction_id', /* ... */);
echo $this->Form->input('2.amount', /* ... */);
echo $this->Form->input('3.account_id', /* ... */);
like image 130
ndm Avatar answered Sep 30 '22 00:09

ndm


Try this:

$splits = TableRegistry::get('splits'); 

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

foreach ($entities as $entity) {
    $splits->save($entity);
}
like image 39
Rana Avatar answered Sep 30 '22 01:09

Rana