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.
0looks misplaced. The magic_idskey 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. - ndm0shouldn't be there at all in your case, the_idskey should be directly connected tosessionplayers. Check the examples in the docs: book.cakephp.org/3.0/en/orm/… - ndm