Save belongsToMany with additional data, three tables and one joining table. (CakePHP 3.5)


#1

Hello!

I’m trying to save a complex belongsToMany association that has additional data but the neccesary _joinData dissapears and I get the following error for join table ElectionResponsibilities:

Cannot insert row, some of the primary key values are missing. Got (174, 7, ), expecting (election_id, user_id, role_id).

I understand the error, but how do I structure the data so that the role_id also is set? The problem started to occur in version 3.4.

The relationship between the tabels are as following:

Elections
	$this->belongsToMany('Users', [
		'through' => 'ElectionsResponsibilities',
	]);
	
Users	
	$this->belongsToMany('Elections', [
		'through' => 'ElectionsResponsibilities',
	]);
	$this->belongsToMany('Roles', [
		'through' => 'ElectionsResponsibilities',
	]);
	
Roles
	$this->belongsToMany('Elections', [
		'through' => 'ElectionResponsibilities',
	]);
	$this->belongsToMany('Users', [
		'through' => 'ElectionResponsibilities',
	]);
	
	
ElectionResponsibilities
	$this->setTable('elections_responsibilities');
	$this->setDisplayField('election_id');
	$this->setPrimaryKey(['election_id', 'user_id', 'role_id']);

	$this->belongsTo('Elections', [
		'foreignKey' => 'election_id',
		'joinType' => 'INNER'
	]);
	$this->belongsTo('Users', [
		'foreignKey' => 'user_id',
		'joinType' => 'INNER'
	]);
	$this->belongsTo('Roles', [
		'targetForeignKey' => 'handle',
		'foreignKey' => 'role_id',
		'joinType' => 'INNER'
	]);

The data structure I’m currently using is:

object(App\Model\Entity\Election) {
		.,
		'users' => [
			(int) 0 => object(App\Model\Entity\User) {
			.,
			'id' => 7,
			'_joinData' => object(App\Model\Entity\ElectionsResponsibility) {

					'role' => object(App\Model\Entity\Role) {

						'handle' => 'admin',
						'status' => 'Administratör',
						'[new]' => false,
						'[accessible]' => [
							'*' => true,
							'handle' => false
						],
						'[dirty]' => [],
						'[original]' => [],
						'[virtual]' => [],
						'[errors]' => [],
						'[invalid]' => [],
						'[repository]' => 'Roles'
					
					},
					'[new]' => true,
					'[accessible]' => [
						'*' => true,
						'election_id' => false,
						'user_id' => false,
						'role_id' => false
					],
					'[dirty]' => [
						'role' => true
					],
					'[original]' => [],
					'[virtual]' => [],
					'[errors]' => [],
					'[invalid]' => [],
					'[repository]' => 'ElectionsResponsibilities',
					},
				'[new]' => false,
				'[accessible]' => [
					'*' => true,
					'id' => false
				],
				'[dirty]' => [
					'_joinData' => true
				],
				'[original]' => [
					'_joinData' => []
				],
				'[virtual]' => [
					(int) 0 => 'presentation_name'
				],
				'[errors]' => [],
				'[invalid]' => [],
				'[repository]' => 'Users'
		
		],
		'[new]' => true,
		'[accessible]' => [
			'*' => true,
			'id' => false
		],
		'[dirty]' => [
			'org_title' => true,
			'title' => true,
			'locator' => true,
			'setting_types' => true,
			'users' => true
		],
		'[original]' => [],
		'[virtual]' => [],
		'[errors]' => [],
		'[invalid]' => [],
		'[repository]' => 'Elections'
}

Any input would be appreciated!