Linking Tables query results

Hi,
every paper, every list, game or test called in our school material.
Every material included competencies.
In add or edit mode I want link the competencies to the material.

in MaterialsTable.php
$this->belongsToMany(‘Competencies’, [
‘className’=>‘Competencies’,
‘through’ => ‘MaterialsToCompetencies’
]);

in CompetenciesTable
$this->belongsToMany(‘Materials’, [
‘className’=>‘Materials’,
‘through’ => ‘MaterialsToCompetencies’
]);

in MaterialsToCompetenciesTable
public function initialize(array $config)
{
$this->belongsTo(‘Competencies’);
$this->belongsTo(‘Materials’);
}

And now in MaterialsController edit I try this…
$compsin = $this->MaterialsToCompetencies->find(‘list’,[‘contain’=>[‘Competencies’=>[‘Parents’]],‘keyField’ => ‘id’,‘valueField’ => ‘label’, ‘conditions’=>[‘MaterialsToCompetencies.material_id’=> $material->id]]);

$comps = $this->Materials->Competencies->find(‘list’,[‘contain’=>[‘Parents’],‘keyField’ => ‘id’,‘valueField’ => ‘label’, ‘conditions’=>[‘Competencies.subject_id =’=> $material->subject_id,‘Competencies.row >’=>1]])->order(‘Competencies.row ASC’)->order(‘Competencies.col ASC’);

$compsin will be the Competencies for one Material in the multiselect field.
$comps worked well and show me all possible competencies for this subject which the material is part from.

in MaterialsToCompetencies.php Entity
protected function _getLabel()
{
return $this->_properties[‘content’];

}

But valueField won’t show my the content of Cometencies or Material (both tables have a content field)
Did anybody have a new approach?

Cheers,
Marc

AnsichtMaterialEdit

“Kompetenzraster” is subject
“Zugeordnete Kompetenzen” is $compsin
“Alle Kompetenzen” is $comps

if I replace ‘valueField’ => ‘label’ with ‘valueField’ => ‘material_id’ in the MaterialController $compsin I’ve got this result.

This was the solution.

In Model/Entity/Competency.php

protected function _getLabel()
{
if($this->_properties[‘lfs’]){
if($this->_properties[‘parent_id’]){
return $this->_properties[‘parents’][‘content’] . '- LFS ’ . $this->_properties[‘lfs’] . ’ - ’ . $this->_properties[‘content’];
}else{
return 'LFS ’ . $this->_properties[‘lfs’] . ’ - ’ . $this->_properties[‘content’];
}
}else{
return $this->_properties[‘content’];
}
}

And in the MaterialsController add and edit:

$compsin = $this->Materials->Competencies->find('list',['contain'=>['Parents'],'conditions'=>'MaterialsToCompetencies.material_id ='.$material->id, 'keyField' => 'id','valueField' => 'label'])->matching('Materials')->order('Competencies.row ASC')->order('Competencies.col ASC');