mistaq
mistaq

Reputation: 385

CakePHP 3 saving associated data across 2 tables

Update: Removed 0 from form input and the s from session, while also changing the relationship from Booking hasMany Sessions to Booking hasOne Session.

So I have 4 tables relevant to this:

When using my form, I attempt to save across Bookings, Sessions & Sessionplayer_sessions. The form input below is the only relevant field for Sessionplayer_sessions:

<?php echo $this->Form->input('session.sessionplayers._ids', ['type' => 'select', 'options'=>'','multiple']); ?>

The options while in the form input show as blank, come from a dual list jQuery script which allows for the selection of multiple items.

However, when I attempt to save, nothing from the above input is actually posted into the Sessionplayer_sessions table, while everything in Bookings & Sessions is successfully posted.

In the Controller, this is what the newEntity & patchEntity sections look like:

$booking = $this->Bookings->newEntity($this->request->data(),[
        'associated'=>[
          'Sessions' => ['associated' => ['Guestengineers', 'Sessionplayers']]
      ]]);
      if($this->request->is('post')){
        $data = $this->request->data;
        $booking = $this->Bookings->patchEntity($booking, $data, [
          'associated'=>[
            'Sessions' => ['associated' => ['Guestengineers', 'Sessionplayers']]
          ]
        ]);

And the respective models of both the Sessions and Sessionplayers tables:

Sessions:

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

    $this->table('sessions');
    $this->displayField('id');
    $this->primaryKey('id');

    $this->addBehavior('Timestamp');

    $this->belongstoMany('Sessionplayers', [
        'foreignKey' => 'session_id',
        'targetForeignKey' => 'sessionplayer_id',
        'joinTable' => 'Sessionplayers_sessions'
    ]);
}

Sessionplayers:

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

    $this->table('sessionplayers');
    $this->displayField('id');
    $this->primaryKey('id');

    $this->addBehavior('Timestamp');

    $this->belongstoMany('Sessions', [
        'foreignKey' => 'sessionplayer_id',
        'targetForeignKey' => 'session_id',
        'joinTable' => 'Sessionplayers_sessions'
    ]);
}

Results:

Upon updating the form input and the relationship between the Bookings & Sessions table and resubmitting:

In the post data, sessionplayers is now an array inside the session array. Inside the sessionplayers array is a single attribute: _ids, with the value referencing the id of the first sessionplayer that was selected in the form input's multiple select - for example, if I selected the 3rd sessionplayer and the 4th sessionplayer, _ids = 3. However, in the database itself, while the bookings and sessions table data was able to be successfully inserted, sessionplayers_session's table did not.

Basically: Request data arrays are as follows: Session (array) -> Sessionplayers (array) -> _ids = 3.

In the SQL log, only two Insert Into SQL statements occurred, one for Bookings and one for Sessions. Sessionplayers_sessions had no such insert.

Upvotes: 0

Views: 895

Answers (1)

mistaq
mistaq

Reputation: 385

I think I managed to fix it after removing the 0 from the form input and changing the relationship from OneToMany to OneToOne, by adding in [] to represent that there could be several items being selected.

Upvotes: -1

Related Questions