# Display data from a matching clause in cakephp3

**URL:** <https://discourse.cakephp.org/t/display-data-from-a-matching-clause-in-cakephp3/509>\
**Category:** Uncategorized\
**Created:** [June 9, 2016, 1:54pm UTC](https://discourse.cakephp.org/t/display-data-from-a-matching-clause-in-cakephp3/509 "2016-06-09T13:54:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jagguy](https://avatars.discourse-cdn.com/v4/letter/j/b487fb/32.png) [@jagguy](https://discourse.cakephp.org/u/jagguy)\
**Post date:** [June 9, 2016, 1:54pm UTC](https://discourse.cakephp.org/t/display-data-from-a-matching-clause-in-cakephp3/509/1 "2016-06-09T13:54:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![karmicdice](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/karmicdice/32/1298_2.png) [@karmicdice](https://discourse.cakephp.org/u/karmicdice)\
**Post date:** [June 10, 2016, 6:03am UTC](https://discourse.cakephp.org/t/display-data-from-a-matching-clause-in-cakephp3/509/2 "2016-06-10T06:03:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jagguy](https://avatars.discourse-cdn.com/v4/letter/j/b487fb/32.png) [@jagguy](https://discourse.cakephp.org/u/jagguy)\
**Post date:** [June 10, 2016, 11:28am UTC](https://discourse.cakephp.org/t/display-data-from-a-matching-clause-in-cakephp3/509/3 "2016-06-10T11:28:37Z")

</div>

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
