I have been trying to figure out this (I guess really simple) thing for several days:
I have three tables with these fields:
My question is:
What should I code in AvadiariesController.php if I want to populate a select box (in an add.ctp) with results from this query:
SELECT
alumnes.nomcognoms
FROM
alumnes_grups
INNER JOIN grups ON
alumnes_grups.grup_id = grups.id
INNER JOIN alumnes ON
alumnes_grups.alumne_id = alumnes.id
WHERE
alumnes_grups.grup_id = '16-17 2nB'
ORDER BY
alumnes.nomcognoms
Can I have a display field from another table? How can I see 'name' instead of 'id'?
User bill kindly suggested something I rewrote (correctly?) to:
$alumnesGrups = $this->Avadiaries->AlumnesGrups->Alumnes->find('list',
['fields' => ['Alumnes.nomcognoms'], // keep the alias consistent with whatever you define in the join
'joins' => [['table' => 'alumnes',
'alias' => 'Alumnes',
'type' => 'INNER',
'conditions' => ['Alumnes.id' => 'AlumnesGrups.alumne_id']],
// mimic the above to join the other table
['table' => 'grups',
'alias' => 'Grups',
'type' => 'INNER',
'conditions' => ['AlumnesGrups.grup_id' => '16-17 2nB'],
'order' => ['Alumnes.nomcognoms' => 'ASC']]
]
]
);
Now add.ctp gives no error, but this condition is not being applied:
'conditions' => ['AlumnesGrups.grup_id' => '16-17 2nB']
I just need this starting point, so I can figure out some other things.
I finally found the solution after reading this document.
$alumnesGrups = $this->Avadiaries->AlumnesGrups->find('all', [
'fields' => ['Alumnes.name'],
'contain' =>['Alumnes', 'Grups'],
'conditions' => ['Grups.id =' => 1]
]);
My problem now is that I'm getting {"Alumnes":{"name":"Angela Smith"}} and not just plain "Angela Smith". I'll keep searching.