How do I get to _matchingData in an Entity?

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,
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


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

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

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 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.