Display data from a matching clause in cakephp3

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.’%’]);
});

How does that matter? It does not affect memory heap - These are objects, not arrays :slight_smile:

yes you are correct, I just didnt like seeing so much data displayed when debugging. If this is the way it is then I will just leave it at that.

Thanks