cakephp 3.x 保存多个实体 - newEntities

cakephp 3.x saving multiple entities - newEntities

我在保存多条记录方面遇到了困难。我已经尝试了一百万件事,但最终遇到了同样的问题:我的记录没有保存,我也看不到任何错误。请记住,我是 cakephp 的新手和新手编码员。

我是否遗漏了一些明显而重要的东西?

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

控制器:

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

模板:

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

调试 $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'

}
]

试试这个例子在cakephp中插入多条记录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 );}

希望对你的问题有用

请重温,谢谢!

试试这个:

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

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

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

您是否曾在某处看到这种 Table.index.field 样式被使用,或者您只是尝试了一些东西并希望它能奏效?

当保存多条记录分别创建多条实体时,预期格式是一个数字索引数组,用于保存各个记录的数据,如文档中所示

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

因此您的输入不应使用 table 名称,而应仅使用索引和字段名称,如

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

如果您想将所有实体作为单个事务处理,您可以使用 transactional():

虽然使用循环,"transations" 避免了 "save" 不起作用时的错误。

    // In a controller.
    $articles->getConnection()->transactional(function () use ($articles, $entities) {
        foreach ($entities as $entity) {
            $articles->save($entity, ['atomic' => false]);
        }
    });

Converting Multiple Records - CakePHP 3