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 TABLEstudents
(
student_id
int(11) NOT NULL,
student_name
varchar(10) NOT NULL,
PRIMARY KEY (student_id
)
)
CREATE TABLEcourses_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
),
KEYfk_course
(course_id
),
KEYfk_student
(student_id
),
CONSTRAINTfk_course
FOREIGN KEY (course_id
) REFERENCEScourses
(course _id
),
CONSTRAINTfk_student
FOREIGN KEY (student_id
) REFERENCESstudents
(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?