# How do I get to \_matchingData in an Entity?

**URL:** <https://discourse.cakephp.org/t/how-do-i-get-to-matchingdata-in-an-entity/11960>\
**Category:** Need Help\
**Created:** [March 29, 2024, 5:46am UTC](https://discourse.cakephp.org/t/how-do-i-get-to-matchingdata-in-an-entity/11960 "2024-03-29T05:46:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![brianko](https://avatars.discourse-cdn.com/v4/letter/b/3da27b/32.png) [@brianko](https://discourse.cakephp.org/u/brianko)\
**Post date:** [March 29, 2024, 5:46am UTC](https://discourse.cakephp.org/t/how-do-i-get-to-matchingdata-in-an-entity/11960/1 "2024-03-29T05:46:09Z")

</div>

**Update** : A search of the forum finds a couple of posts where folks are actually _recommending drilling down through \_joinData or \_matchingData._ Not only is this behavior undocumented (I’m not finding it in the 5.x docs), but it’s bad mojo to depend upon private variable access to implement functionality. I’m having a hard time believing this is the accepted answer.

**Original question** : I have a simple test setup with two tables and an association table:

> CREATE TABLE `courses` (  
> `course_id` int(11) NOT NULL,  
> `course_name` varchar(10) DEFAULT NULL,  
> PRIMARY KEY (`course_id`)  
> )  
> CREATE TABLE `students` (  
> `student_id` int(11) NOT NULL,  
> `student_name` varchar(10) NOT NULL,  
> PRIMARY KEY (`student_id`)  
> )  
> CREATE TABLE `courses_students` (  
> `id` mediumint(8) unsigned NOT NULL AUTO\_INCREMENT,  
> `grade` int(11) NOT NULL,  
> `course_id` int(11) NOT NULL,  
> `student_id` int(11) NOT NULL,  
> PRIMARY KEY (`id`),  
> KEY `fk_course` (`course_id`),  
> KEY `fk_student` (`student_id`),  
> CONSTRAINT `fk_course` FOREIGN KEY (`course_id`) REFERENCES `courses` (`course _id`),  
> CONSTRAINT `fk_student` FOREIGN KEY (`student_id`) REFERENCES `students` (`stu dent_id`)  
> )

I used bake to generate all code.

If I use

> $query = $this-\>Students-\>find(‘all’, contain:[‘Courses’]);

the associated field “grade” is buried in a private attribute called \_joinData. If I use

> find()-\>matching()

, it’s buried in \_matchingData. How do I access the data field from the association without going through either \_joinData or \_matchingData?

---

<div class="post-metadata">

**Author:** ![brianko](https://avatars.discourse-cdn.com/v4/letter/b/3da27b/32.png) [@brianko](https://discourse.cakephp.org/u/brianko)\
**Post date:** [March 31, 2024, 1:51am UTC](https://discourse.cakephp.org/t/how-do-i-get-to-matchingdata-in-an-entity/11960/2 "2024-03-31T01:51:32Z")

</div>

Judging from the silence, I guess there isn’t a better way.

---

<div class="post-metadata">

**Author:** ![KevinPfeifer](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/kevinpfeifer/32/3365_2.png) [@KevinPfeifer](https://discourse.cakephp.org/u/KevinPfeifer)\
**Post date:** [March 31, 2024, 10:33am UTC](https://discourse.cakephp.org/t/how-do-i-get-to-matchingdata-in-an-entity/11960/3 "2024-03-31T10:33:51Z")

</div>

Instead of doing a belongsToMany you can always just convert your N:M relation to 2 1:N relations and therefore have a separate table and entity class as your middleman.

A N:M relation (aka belongsToMany) has the characteristic, that Table A directly has access to associated entities in Table B via Table C. How else would you like to have these fields in Table C be populated in PHP? Inside the entities of Table B like they were fields in Table B?

If you can think of a better solution you can always just prepare a PR on the 5.next branch and try to confince the core team to merge it. Otherwise you can always create your own plugin (either in the plugins directory or a public plugin via composer) and overwrite any part of the framework to your liking if you feel you need it.

* * *

Also be aware, that the Cake community isn’t “huge” and therefore not even waiting for 2 days and calling it silence since no one had the time during easter holidays to respond to your question is just dumb.
