Cakephp 3.x saving multiple objects - newEntities

My hardest part is saving a few records. I tried a million things, but in the end I have the same problem: my records are not saved, and I do not see any errors. Keep in mind that I am new to cakephp and new to encoder.

Did I miss something obvious and important?

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

Debugging 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'

}
]
+4
source share
3 answers

Have you ever seen this style Table.index.field, or did you just try something and hope that it works?

, , , ,

Cookbook > ORM > >

, / , newEntities():

[...]

:

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

, , ,

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', /* ... */);
+5

:

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

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

foreach ($entities as $entity) {
    $splits->save($entity);
}
+1

, cakphp 3.x

$passwords = $this->request->data('password_id');
foreach ($passwords as $password) {
$data = [
    'event_id'    => $this->request->data('event_id'),
    'password_id' => $password
];
$eventPasswordAll = $this->EventPasswordAll->newEntity();
$this->EventPasswordAll->patchEntity($eventPasswordAll, $data);
$this->EventPasswordAll->save($eventPasswordAll );}

,

, , !

0

Source: https://habr.com/ru/post/1610016/


All Articles