我正在尝试将多个游戏与我的数据库中的连续性相关联。
我有 3 个表:continuities、continuities_games 和 games。 continuities_games 表有一个continuity_id 和一个game_id。当我尝试在我的选择输入中选择不同的游戏时,它会在数据库中创建一个新的游戏条目,然后在连接表中创建一个条目,将其链接到该新游戏条目,而不是将其链接到最初选择的游戏。
这是我的 ContinuitiesTable 中的初始化函数:
public function initialize(array $config){
parent::initialize($config);
$this->belongsToMany('Games');
}
对于我的 GamesTable:
public function initialize(array $config){
parent::initialize($config);
$this->displayField('name');
$this->addBehavior('Tree');
//Associations
$this->hasMany('Children', [
'className' => 'Games',
'foreignKey' => 'parent_id',
'dependant' => true,
'cascadeCallbacks' => true,
]);
$this->belongsTo('Parent', [
'className' => 'Games',
]);
$this->hasMany('Books');
$this->belongsToMany('Continuities');
}
这是我在 ContinuitiesController 中的编辑功能:
public function edit($id=null){
$continuity = $this->Continuities->find()->where(['id'=>$id])->contain('Games')->first();
if ($this->request->is(['post', 'put'])) {
debug($this->request->data);
$continuity->start_date = $this->request->data['start_date']['year'].'-'.$this->request->data['start_date']['month'].'-'.$this->request->data['start_date']['day'];
$continuity->end_date = $this->request->data['end_date']['year'].'-'.$this->request->data['end_date']['month'].'-'.$this->request->data['end_date']['day'];
//$continuity->start_date = $this->request->data['start_date'];
//$continuity->end_date = $this->request->data['end_date'];
$continuity->games = $this->request->data['games'];
debug($this->Continuities->newEntity($this->request->data)->toArray());
debug($continuity);
if ($result = $this->Continuities->save($continuity, ['associated'=>['Games']])) {
$this->Flash->success(__('The continuity has been updated.'));
return $this->redirect(['action' => 'view', $continuity->id]);
}
$this->Flash->error(__('Unable to update the continuity.'));
}
$games = $this->Continuities->Games->getGamesList();
$this->set(compact('continuity', 'games'));
}
最后是 edit.ctp 表单:
<?= $this->Form->create($continuity) ?>
<fieldset>
<legend><?= __('Edit Continuity') ?></legend>
<?= $this->Form->input('start_date') ?>
<?= $this->Form->input('end_date') ?>
<?= $this->Form->input('games', ['multiple'=>true]) ?>
</fieldset>
<?= $this->Form->button(__('Submit')); ?>
<?= $this->Form->end() ?>
谢谢。
最佳答案
有一些事情需要改变才能使这项工作发挥作用。
首先是 Continuities 表:
public function initialize(array $config){
parent::initialize($config);
$this->belongsToMany('Games',
[
'targetForeignKey' => 'game_id',
'foreignKey' => 'continuity_id',
'joinTable' => 'continuities_games',
]);
}
接下来是 Games 表:
public function initialize(array $config){
parent::initialize($config);
$this->belongsToMany('Continuities',
[
'targetForeignKey' => 'continuity_id',
'foreignKey' => 'game_id',
'joinTable' => 'continuities_games',
]);
}
现在,ContinuitiesController 的 edit() 函数:
public function edit($id=null){
$continuity = $this->Continuities->find()->where(['id'=>$id])->contain('Games')->first();
if ($this->request->is(['post', 'put'])) {
$this->Continuities->patchEntity($continuity, $this->request->data(), ['associated'=>['Games']]);
if ($result = $this->Continuities->save($continuity, ['associated'=>['Games']])) {
$this->Flash->success(__('The continuity has been updated.'));
return $this->redirect(['action' => 'view', $continuity->id]);
}
$this->Flash->error(__('Unable to update the continuity.'));
}
$games = $this->Continuities->Games->getGamesList();
$this->set(compact('continuity', 'games'));
}
最后,edit.ctp
<?= $this->Form->create($continuity) ?>
<fieldset>
<legend><?= __('Edit Continuity') ?></legend>
<?= $this->Form->input('start_date') ?>
<?= $this->Form->input('end_date') ?>
<?= $this->Form->input('games._ids', ['options' => $games, 'multiple'=>true]) ?>
</fieldset>
<?= $this->Form->button(__('Submit')); ?>
<?= $this->Form->end() ?>
关于cakephp - 在 Cakephp 3 中使用 BelongsToMany 关联,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26023045/