I have a number of situations where a join table has extra data in it, not just the foreign keys. For example, I have teams
and people
, and teams_people
which has things like what their role is on the team (captain, player, etc.), their number, and so on. To date, I have been loading team rosters with something like $teams = $this->Teams->find()->where([...])->contain(['People']);
and then as I’m iterating through the people on a team I access the join table record through $person->_joinData->role
for example.
I’ve been told by a trustworthy source that _joinData
should be an implementation detail that my code is unaware of, and I should change my containment so that the data is present in a more reliable location, which will probably require ensuring that TeamsTable
and PeopleTable
both have a hasMany
association to TeamsPeopleTable
.
Assuming that I know how to set up all the required associations, what should my find
statement above be changed to, in order to ensure all the data is present?
->contain(['TeamsPeople' => 'People'])
? I don’t love that, as it means that the Team
entities loaded this way will not have a people
property, but any that are loaded in places where I might need the list of people but nothing about their roles, etc. would. That makes the Team
entity a little bit unpredictable in terms of common functions that might be used for both scenarios.
->contain(['People' => 'TeamsPeople'])
? I think that will load all the join table records for each person, regardless of what team they are on?