在Cakephp 3中使用BelongsToMany关联

时间:2014-09-24 17:34:01

标签: cakephp cakephp-3.0

我正在尝试将多个游戏与我的数据库中的连续性相关联。

我有3个表:连续性,连续性游戏和游戏。 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() ?>

感谢。

1 个答案:

答案 0 :(得分:5)

为了实现这一目标,有一些事情需要改变。

首先是Continuities表:

public function initialize(array $config){
    parent::initialize($config);

    $this->belongsToMany('Games', 
        [
            'targetForeignKey' => 'game_id',
            'foreignKey' => 'continuity_id',
            'joinTable' => 'continuities_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')); 
}

最后,编辑.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() ?>