Using the BelongsToMany Association in Cakephp 3

I am trying to link multiple games with continuity in my database.

I have 3 tables: continuities, continuities_games and games. The continuions_games table has a continuity_id and a game_id. When I try to select different games in my input, it creates a new entry in the database and then creates an entry in the joins table linking it to this new game, instead of linking it to the originally selected game.

This is the initialization function in my ContinuitiesTable:

public function initialize(array $config){
    parent::initialize($config);
    $this->belongsToMany('Games');
}

      

And for my 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');
}

      

This is my edit function in 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')); 
}

      

And finally, the edit.ctp form:

<?= $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() ?>

      

Thank.

+3


source to share


1 answer


There were a few things that needed to be changed to make this work.

First, the account assignment table is found:

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

    $this->belongsToMany('Games', 
        [
            'targetForeignKey' => 'game_id',
            'foreignKey' => 'continuity_id',
            'joinTable' => 'continuities_games',
        ]);
}

      

Next, the Games table:

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

    $this->belongsToMany('Continuities',
        [
            'targetForeignKey' => 'continuity_id',
            'foreignKey' => 'game_id',
            'joinTable' => 'continuities_games',
        ]);
}

      



Now the edit () ContinuitiesController function:

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')); 
}

      

Finally, 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() ?>

      

+5


source







All Articles