gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 17:46:33 27 4
gpt4 key购买 nike

我最难的是保存多条记录。我已经尝试了一百万次,但最终还是遇到了同样的问题:我的记录没有保存,我看不到任何错误。请记住,我是 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'
]);

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

模板:
        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'

}
]

最佳答案

你在什么地方看到过这个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
],
];


所以你的输入不应该使用表名,而应该使用索引和字段名,比如
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,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32912941/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com