php - Cakephp best way to store count of children's children -
ok if student belongs course , course in turn belongs school:
what's best way store/get count of students in school?
i've discovered countercache work if student belonged directly school. thinking of doing manual count adding course students in foreach loop. there neater way?
i'd preferably store student_count field in school table that's how storing of school's other counts (course, module, lecturer), may fussy ocd kicking in.
according question, try following code:
function test(){ $this->loadmodel('student'); $joins = array( array( 'table' => 'courses', 'alias' => 'course', 'type' => 'left', 'conditions' => array('course.id = student.course_id') ), array( 'table' => 'schools', 'alias' => 'school', 'type' => 'left', 'conditions' => array('school.id = course.school_id') ), ); $school_wise_student_count = $this->student->find('all', array('fields'=>array('count(student.id) total_student','school.name school_name'), 'joins'=>$joins, 'group'=>array('course.school_id'))); pr($school_wise_student_count); die; }
Comments
Post a Comment