在CakePHP 3.0中将额外数据保存到联合表中

3

我正在尝试使用CakePHP将一些数据保存到联合表中。这是我想要修复的应用程序的一部分 - 它是一个带有额外列的常规BelongsToMany关联:

模型 > 实体:

/* Durations */
class Duration extends Entity {
  protected $_accessible = [
    'duration' => true,
    'cost' => true,
  ];
}

/* Commercials */
class Commercial extends Entity {

  protected $_accessible = [
    'info' => true,
    'commercial_durations' => true,
  ];
}

/* CommercialDurations */
class CommercialDuration extends Entity {
  protected $_accessible = [
    'duration_id' => true,
    'commercial_id' => true,
    'quantity' => true,
    'duration' => true,
    'commercial' => true,
  ];
}

模型 > 表:

class DurationsTable extends Table {
  public function initialize(array $config)
  {
    $this->table('durations');
    $this->displayField('id');
    $this->primaryKey('id');

    $this->belongsToMany('Commercials', [
        'through' => 'CommercialDurations',
    ]);
  }
}

class CommercialsTable extends Table
{
  public function initialize(array $config){
    $this->table('commercials');
    $this->displayField('id');
    $this->primaryKey('id');

    $this->belongsToMany('Durations', [
        'through' => 'CommercialDurations'
    ]);

    $this->hasMany('CommercialDurations', [
        'foreignKey' => 'commercial_id'
    ]);

  }
}

class CommercialDurationsTable extends Table {
  public function initialize(array $config)
  {
    $this->table('commercial_durations');
    $this->displayField('id');
    $this->primaryKey('id');
    $this->belongsTo('Durations', [
        'foreignKey' => 'duration_id',
        'joinType' => 'INNER'
    ]);
    $this->belongsTo('Commercials', [
        'foreignKey' => 'commercial_id',
        'joinType' => 'INNER'
    ]);
  }
}

现在,我创建了一个新的视图,希望人们能够选择一个持续时间,输入数量并将该值添加到数据库中。我正在使用以下代码:

<?php 
  echo $this->Form->create($commercial); 
    echo $this->Form->input('durations._duration', ['options' => $durations]);
    echo $this->Form->input('durations._joinData.quantity'); 
    echo $this->Form->submit(__('Next'), ['class' => 'button small right', 'escape' => false]);
  echo $this->Form->end() 
?>

这个表单的问题在于,持续时间选择器未显示来自“持续时间”表的“持续时间”字段,而是将该表中所有字段(每行一个)作为JSON显示。
<option value="0">{ "id": 1, "duration": "30 sec", "cost": 450 }</option>

当我提交表单后,无法将此信息保存到Commercials对象或CommercialDurations中。以下是我从 $this->request->data 对象获取的内容:

[
'durations' => [
    '_duration' => '2',
    '_joinData' => [
        'quantity' => '2'
    ]
  ] 
]

在我开始表格之前,debug((string)$commercial)的输出是:
/src/Template/Commercials/features.ctp (line 22)
'{
  "id": 2,
  "info": "AAAAAA ",
  "created": "2015-04-16T21:48:48+0000",
  "updated": null,
  "durations": [],
}'

如何在表单上正确显示数据?如何检索和保存这些数据?谢谢!

请在调用$this->Form->create($commercial)之前展示debug((string)$commercial)的结果。 - José Lorenzo Rodríguez
@JoséLorenzo,我已将此信息添加到原始问题中。谢谢。 - jpruizs
1
{ "id": 1, "duration": "30 sec", "cost": 450 } 这个是从哪里来的?你的实体中没有关于 durations 的任何信息。 - José Lorenzo Rodríguez
我认为@JoséLorenzo是正确的,应该使用'durations' => true而不是'commercial_durations' => true。谢谢!我将在下面添加完整答案。 - jpruizs
1个回答

0

我不理解你的模型关系,因为根据你的帖子,持续时间属于商业活动,而商业活动又属于持续时间。

为了说明请求应该如何发送以及表单的样子,让我们假设你的模型如下:

你的商业活动有一个商业活动持续时间,而这个商业活动持续时间属于一个持续时间

所以你的模型应该像这样:

  • 商业活动有很多商业活动持续时间。

  • 商业活动持续时间属于商业活动。

  • 商业活动持续时间属于持续时间。

  • 持续时间有很多商业活动持续时间。

你的添加函数应该像这样(假设你不保存新的持续时间,只保存商业活动和商业活动持续时间)

    $commercial = $this->Commercials->newEntity($this->request->data,[
        'associated' => ['Commercialdurations']
    ]);
    if ($this->Commercials->save($commercial)) {
    $success = true;
    }else{
    $success = false;
    }

请求数据应该长这样:

{  
   "commercialdurations":{  
      "Commercialduration":{
      "duration_id":"1",
      "quantity":"1",
      "duration":"1"  
      }
   },
   "info":"something"
}

基本上,您正在发送新商业(信息)的数据以及与之相关联的商业持续时间(您可以发送多个商业持续时间)。

为了使您的表单显示持续时间,您需要在控制器中序列化此信息,然后转到添加操作并添加此信息。(您可以使用任何方式检索数据,重要的是将其发送回视图)

    $durations = $this->Commercials->Commercialdurations->Durations->find('list', ['limit' => 200]);
    $this->set(compact('commercial',durations'));
    $this->set('_serialize', ['commercial']);

然后在您的表单中,您可以使用这些数据

echo $this->Form->input('duration_id', ['options' => $durations]);

因此,您的表单将类似于以下内容:

    echo $this->Form->input('info');
    echo $this->Form->input('commercials.Commercial.duration_id', ['options' => $durations]);
    echo $this->Form->input('commercials.Commercial.quantity');
    echo $this->Form->input('commercials.Commercial.duration');

基本上,您想要发送一个请求,其中包含所有相关数据的级别。

有关保存关联数据的指南,请参阅此其他问题:

Cake PhP 3 Saving associated data in multiples levels

要了解有关如何构建表单的更多信息:

http://book.cakephp.org/3.0/en/views/helpers/form.html#creating-inputs-for-associated-data


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接