Monday, July 2, 2012

Retrieving information with a hasMany through (The Join Model) association.

Hey everyone,

I'm I have a hasMany through (The Join Model) association on my application. I have 3 models in my application: students, courses and the join model: membership (used as a join model and also tracks grades). I'm trying to retrieve some information from the database, I want to get one course information along with all of it's students and their grades by calling: $this->Course->Membership->findAllByCourseId($id). In return I'm getting the following:
Array
(
    [0] => Array
        (
            [Membership] => Array
                (
                    [id] => 12
                    [student_id] => 9
                    [course_id] => 3
                    [grade] => 100
                )
            [Student] => Array
                (
                    [id] => 9
                    [name] => Kyle
                    [created] => 2012-07-02 10:32:24
                    [modified] => 2012-07-02 12:44:37
                )
            [Course] => Array
                (
                    [id] => 3
                    [name] => Math
                    [created] => 2012-07-02 10:31:17
                    [modified] => 2012-07-02 10:31:17
                )
        )
    [1] => Array
        (
            [Membership] => Array
                (
                    [id] => 14
                    [student_id] => 10
                    [course_id] => 3
                    [grade] => 86
                )
            [Student] => Array
                (
                    [id] => 10
                    [name] => Butters
                    [created] => 2012-07-02 10:32:29
                    [modified] => 2012-07-02 12:44:41
                )
            [Course] => Array
                (
                    [id] => 3
                    [name] => Math
                    [created] => 2012-07-02 10:31:17
                    [modified] => 2012-07-02 10:31:17
                )
        )
)
As you can see, the course information is duplicated across the whole array. Because the course information is the same for all of them, it's just pointless and wastes resources.
So, is there a way to retrieve data like that, by a specific parameter having information duplicated across the entire array?

Also, is there a way to use a HABTM association that also saves information such as: grades or days attended?

Thanks!

--
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: