I need to make some report of my multiple associations, I tried with
containable and was surprised as it resolve to bring all the data with
the proper joins, however still do multiple queries for a model that
is already joined.
$params = array(
'fields' => array(
'id', 'student_id', 'course_id',
'registration_status_id',
'registration_course_status_id', 'progress'
),
'contain' => array(
'Course' => array(
'fields' => array('id', 'title')
),
'Student' => array(
'fields' => array('id', 'firstname', 'lastname'),
'City',
'Country'
),
'CourseTracking' => array(
'fields' => array('started', 'finished')
)
),
'conditions' => array(
),
'order' => array('Student.lastname')
);
$this->Course->Registration->find('all', $params);
I have the Registration model which is a link table between Course and
Student, and the generated query is:
SELECT `Registration`.`id`, `Registration`.`student_id`,
`Registration`.`course_id`, `Registration`.`registration_status_id`,
`Registration`.`registration_course_status_id`,
`Registration`.`progress`, `CourseTracking`.`started`,
`CourseTracking`.`finished`, `Course`.`id`, `Course`.`title`,
`Student`.`id`, `Student`.`firstname`, `Student`.`lastname`,
`Student`.`country_id`, `Student`.`city_id` FROM `registrations` AS
`Registration` LEFT JOIN `courses` AS `Course` ON
(`Registration`.`course_id` = `Course`.`id`) LEFT JOIN `students` AS
`Student` ON (`Registration`.`student_id` = `Student`.`id`) LEFT JOIN
`course_trackings` AS `CourseTracking` ON
(`CourseTracking`.`registration_id` = `Registration`.`id`) WHERE
course_id IN (1, 2) ORDER BY `Student`.`lastname` ASC
Which have all the required data, however the sql dump show me one of
the following queries for every registration:
SELECT `Course`.`id`, `Course`.`title` FROM `courses` AS `Course`
WHERE `Course`.`id` = 2
The strange part is that Course and Student are associated exactly in
the same way. Any hint on how to get rid of those queries?
--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.
To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php
No comments:
Post a Comment