I want to display data from a belongsToMany relationship. I have lessons table with belongstomany students table. I used matching to get rows I need from the student table and it works BUT I am also getting the entire student table returned and not the just 3 selected student table fields as required.
How do I select only a few fields from the student table and display this in a view ? Below is the outputs of the data and code snippets
To display the student data I use the below
<?= $bookmark->students[0]->first_name ?> <?= $bookmark->students[0]->last_name ?>
from the output of the debug you see I get the students table as well which I dont want. the matching data is all I need
‘_matchingData’ => [
‘Students’ => [
‘id’ => (int) 162,
‘first_name’ => ‘Mila’,
‘last_name’ => ‘Rors’
]
]
…
‘students’ => [
(int) 0 => [
‘id’ => (int) 162,
‘student_inactive’ => false,
‘student_enq’ => false,
…
This is the query
$query3 = $this->Lessons->find()
->contain([‘Tutors’,‘Subjects’, ‘TutoringTypes’,‘Terms’,‘Students’])
->select([‘Lessons.id’,‘Lessons.lesson_date’,‘Tutors.id’
,‘Tutors.first_name’,‘Tutors.last_name’,
‘Subjects.name’,‘TutoringTypes.value’])
->where(['Lessons.lesson_date ’ => $a3 ]);
$query3->hydrate(true);$query3->matching(‘Students’, function ($q) use ($a8) {
return $q
->select([‘Students.id’,‘Students.first_name’,‘Students.last_name’])
->where([‘Students.first_name like’ =>‘%’.$a8.‘%’]);
});
…