【问题标题】:cakephp 3.x saving multiple entities - newEntitiescakephp 3.x 保存多个实体 - newEntities
【发布时间】:2015-10-02 17:47:29
【问题描述】:

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

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

表:

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

}
]

【问题讨论】:

  • 数据没有插入到表中对吗?
  • 替换 SaveAll 而不是保存在您的控制器中。
  • @KarthikKeyan CakePHP 3.x 中没有saveAll()
  • 您是否检查我们的实体中是否存在验证错误?

标签: cakephp cakephp-3.0 entities


【解决方案1】:

您是否在某个地方看到过这种Table.index.field 样式正在使用,或者您是否只是尝试了一些东西并希望它会起作用?

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

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

创建一次创建/更新多条记录的表单时,您 可以使用 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', /* ... */);

【讨论】:

  • NDM:我确实在某处看到了 Table.index.field 语法。我想我被验证错误挂断了,当我应该更加注意正确格式化数据时。感谢您的帮助。
【解决方案2】:

试试这个:

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

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

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

【讨论】:

  • 也许他想要一个无循环的解决方案,就像早期版本的 CakePHP 有 SaveAll() 方法...
【解决方案3】:

试试这个例子在 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 );}

希望对你的问题有用

请重温一下,谢谢!

【讨论】:

  • 也许他想要一个无环的解决方案。
【解决方案4】:

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

虽然使用循环,但如果“保存”不起作用,“事务”可以避免错误。

// 在控制器中。 $articles->getConnection()->transactional(function() 使用 ($articles, $entities) { foreach ($entities 作为 $entity) { $articles->save($entity, ['atomic' => false]); } });

Converting Multiple Records - CakePHP 3

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多