Wednesday, November 30, 2011

HABTM & read.

Hi !
I have a little problem with the HABTM associations.

Group hasManyAndBelongsTo Lesson
Lesson hasManyAndBelongsTo Group

Groups
groups.id | groups.name | ...

Lessons
lessons.id | lessons.title | ...

Groups_Lessons
groups_lessons.id | groups_lessons.lesson_id | groups_lessons.group_id

I have write this the Lesson model :

class Lesson extends AppModel {
public $name = 'Lesson';
public $hasAndBelongsToMany = array(
        'Groups' =>
            array(
                'className'              => 'Group',
                'joinTable'              => 'groups_lessons',
                'foreignKey'             => 'lesson_id',
                'associationForeignKey'  => 'group_id'
            )
    );
}

and this Group model :

class Group extends AppModel {
public $name = 'Group';

public $hasAndBelongsToMany = array(
        'Lessons' =>
            array(
                'className'              => 'Lesson',
                'joinTable'              => 'groups_lessons',
                'foreignKey'             => 'group_id',
                'associationForeignKey'  => 'lesson_id',
            )
    );

}

In the index of my lesson controller, I realize a $this->Lesson->find('all') call.
I have 2 SQL query :

SELECT "Lesson"."id" AS "Lesson__id", "Lesson"."teacher_id" AS "Lesson__teacher_id", "Lesson"."entitled" AS "Lesson__entitled", "Lesson"."abbreviation" AS "Lesson__abbreviation", "Lesson"."hours" AS "Lesson__hours", "Lesson"."option" AS "Lesson__option" FROM "lessons" AS "Lesson" WHERE 1 = 1

> One row 

SELECT "Groups"."id" AS "Groups__id", "Groups"."holder_id" AS "Groups__holder_id", "Groups"."education_id" AS "Groups__education_id", "Groups"."name" AS "Groups__name", "Groups"."abbreviation" AS "Groups__abbreviation", "GroupsLesson"."id" AS "GroupsLesson__id", "GroupsLesson"."group_id" AS "GroupsLesson__group_id", "GroupsLesson"."lesson_id" AS "GroupsLesson__lesson_id" FROM "groups" AS "Groups" JOIN "groups_lessons" AS "GroupsLesson" ON ("GroupsLesson"."lesson_id" = 1 AND "GroupsLesson"."group_id" = "Groups"."id")

> One row 

But, when I realize a print_r on  $this->Lesson->find('all'), I have no group...

Array ( [0] => Array ( [Lesson] => Array ( [id] => 1 [teacher_id] => 1 [entitled] => Math [abbreviation] => M [hours] => 14 [option] => ) [Groups] => Array ( ) ) )

Can you help me ?
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: