0
votes

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:

  • Bookings (where the form with the input fields is from)
  • Sessions (linked to Bookings)
  • Sessionplayers_sessions (joined table between Sessionplayers & Sessions)
  • Sessionplayers

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.

1
The 0 looks misplaced. The magic _ids key is supposed to be connected directly to the association property name. Also, please always try to do a little debugging, as questions that are basically just "doesn't work" are usually considered offtopic. In your case checking what exactly the request data and the patched entity looks like in your controller is probably most relevant. - ndm
Oh I did that. Upon submitting, in Request, the join table's attributes weren't present in the post data. I also tried the Cookbook's through option for the many to many association, but there was no difference. When you say the 0 is misplaced, where is 0 normally meant to go relative to associated tables? - mistaq
I didn't just mean to say do some debugging, but especially ment that the results should be in your question. I'd say the 0 shouldn't be there at all in your case, the _ids key should be directly connected to sessionplayers. Check the examples in the docs: book.cakephp.org/3.0/en/orm/… - ndm
Updated my post with the results after removing the 0 and changing the relationship between the bookings and sessions table. - mistaq
I think I managed to fix it - I added an array [] right after _ids, and they seem to have saved properly, although I did get several warnings upon saving. - mistaq

1 Answers

-1
votes

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.