【问题标题】:Saving Additional Data to a Joint Table in CakePHP 3.0在 CakePHP 3.0 中将附加数据保存到联合表中
【发布时间】:2015-06-27 19:48:17
【问题描述】:

我正在尝试使用 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-&gt;Form-&gt;create($commercial)之前显示debug((string)$commercial)的结果
  • @JoséLorenzo 我将此信息添加到原始问题中。谢谢。
  • { "id": 1, "duration": "30 sec", "cost": 450 } 来自哪里?您的实体中没有任何关于 durations 的内容
  • 我认为你是对的 @JoséLorenzo 而不是 'commercial_durations' => true,我添加了 'durations' => true, 。谢谢!我将在下面添加完整答案

标签: php forms cakephp cakephp-3.0


【解决方案1】:

我不了解您的模特关系,因为根据您的帖子,持续时间属于商业广告,而商业广告属于持续时间。

为了向您解释应如何发送请求以及表单的外观,让我们假设此示例中您的模型如下所示:

您的广告有一个商业时长,而这个商业时长属于一个时长

所以你的模型看起来像这样:

  • Commercial 有很多商业期限。

  • 商业时长属于商业。

  • 商业时长属于时长。

  • 时长有很多商业时长。

您的添加功能应该是这样的(假设您没有保存新的持续时间,只是商业和商业持续时间)

    $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

【讨论】:

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