【问题标题】:Add multiple record in add controller in cakephp 3在 cakephp 3 的添加控制器中添加多条记录
【发布时间】:2018-10-03 05:26:45
【问题描述】:

我必须在添加表单(控制器)中添加相同的多行,但它不会保存多条记录。我必须在foreach下移动保存吗?或者我可以做些什么来保存多行?

$boldNumberRequest = $this->BoldNumberRequest->newEntity();                                                          
$boldNumberRequest = $this->BoldNumberRequest->patchEntity($boldNumberRequest, $this->request->data);
$this->BoldNumberRequest->saveMany($boldNumberRequest)

我在 $this->request->data 中得到了什么

(int) 0 => [
    'bold_number_from' => '1',
    'bold_number_to' => '1000',
    'is_approved' => '1',
    'unique_series' => '030318',
    'owner_company_id' => '1'
],
(int) 1 => [
    'bold_number_from' => '1',
    'bold_number_to' => '1000',
    'is_approved' => '1',
    'unique_series' => '100318',
    'owner_company_id' => '1'
],

【问题讨论】:

    标签: cakephp-3.0 cakephp-3.x cakephp-3.4


    【解决方案1】:

    您的问题是您正在使用 $this->BoldNumberRequest->newEntity() 创建单个实体,然后尝试使用多个实体的数据对其进行修补。

    你想要的可以在关于Saving Multiple Entities的CakePHP文档中找到:

    您需要从数据中创建多个实体:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多