cakephp 3.x保存多个实体-newEntities [英] cakephp 3.x saving multiple entities - newEntities

查看:125
本文介绍了cakephp 3.x保存多个实体-newEntities的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难保存多条记录。我已经尝试了上百万种方法,但最终还是遇到了同样的问题:我的记录未保存,并且看不到任何错误。请记住,我是Cakephp的新手和新手。

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?

表:

    $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:

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'

}
]


推荐答案

您是否曾经在某处看到正在使用此 Table.index.field 样式,或者您是否只是尝试过一些东西并希望它能工作?

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 >数据库访问和ORM>保存数据>转换多个记录

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


创建一次创建可同时创建/更新多个记录的表单时
可以使用newEntities():

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

这篇关于cakephp 3.x保存多个实体-newEntities的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆