php - Complex Associations - CakePHP 3.x [ Nested belongsToMany] -
i have complex joining like
a belongs many b threw ab table ab belongs many c threw abc table
now problem how can data in controller contain result till table abc. not finding direct relation in , table abc
my database structure is
table (id, name, code) table b (id, name, code) table c (id, name, code) table ab (id, a_id, b_id, fee) table abc (ab_id, c_id, opt_date, tno)
i can connect b using below query not sure how data abc
$this->a->find('list',[ contain => ['b'] ]);
here changing real scheme
1. courses (id, name, course_code, etc...) 2. sessions (id, name, status, start_date, end_date) 3. seats (id, name, description, metadata, status) 4. courses_sessions (id, course_id, session_id, programme_coordinator, academic_coordinator, status) 5. courses_sessions_seats (id, courses_session_id, seats_id, no_seats)
current relations
a) courses belongstomany sessions using courses_sessions b) courses_sessions belongstomany seats using courses_sessions_seats
i don't know if case 2 possible on not, want show course details available seats per category session wise.
in short this
$this->a ->find() ->contain([ 'b' => ['c'] ]);
or
$this->a ->find() ->contain([ 'b' => function($q) { return $q->contain(['c']); }]);
you can read here in docs
edit
what think table schema
- courses_sessions
- course_sessions belongsto course
- course_sessions belongsto session
- courses_sessions_seats
- courses_sessions_seats belongsto course_sessions
- courses_sessions_seats belongsto seats
if make models in way, have more control on queries later.
edit2
to obtain data perspective of courses case use
$courses = $this->courses ->find() ->contain([ 'coursessessions' => ['sessions', 'sessionsseats'] ]); foreach ($courses $course) : // code here endforeach;
it easy
Comments
Post a Comment