Hello everybody.
I have an issue with reading data via a HABTM relation in CakePHP.
I searched through a lot of stuff on the net and the manual did not
give a straight answer either, this is why I am writing here now.

I have two models (user, course) that are connected in two ways to
each other.
The Corresponding tables look like this:
users
->id
->name
->email
->password

courses
->id
->teacher
->name

The teacher column in courses is a Foreign Key to the users->id
I have linked the two models to represent the teacher relation in
cakePHP like this:
user.php
var $hasMany = array('Course' =>
                             array('className'    => 'Course',
                                   'conditions'   => '',
                                   'order'        => 'Course.name',
                                   'limit'        => '',
                                   'foreignKey'   => 'teacher',
                                   'dependent'    => true,
                                   'exclusive'    => false,
                                   'finderQuery'  => ''
                                  ));

course.php
var $belongsTo = array('Teacher' =>
                               array('className'  => 'User',
                                     'conditions' => '',
                                     'order'      => '',
                                     'foreignKey' => 'teacher'
                                    ));

Now all of this works just fine, however I believed this to be
relevant for the following:
There is another relation between User and Course: Student
This relation is hasAndBelongsToMany (n:m)
The table resolving the connection looks like this:
students
->student
->course
student is an FK to id in users and course is an FK to id in courses.

I have represented this in cakePHP like this:
user.php
var $hasAndBelongsToMany = array('Course' =>
                                         array('className'             =>
'Course',
                                               'joinTable'             =>
'students',
                                               'foreignKey'            =>
'student',
                                               'associationForeignKey' =>
'course',
                                               'conditions'            => '',
                                               'order'                 =>
'Course.name',
                                               'limit'                 => '',
                                               'unique'                =>
false,
                                               'dependent'             =>
true,
                                               'finderQuery'           => '',
                                               'deleteQuery'           => ''
                                              ));

course.php
var $hasAndBelongsToMany = array('Student' =>
                                         array('className'             =>
'User',
                                               'joinTable'             =>
'students',
                                               'foreignKey'            =>
'course',
                                               'associationForeignKey' =>
'student',
                                               'conditions'            => '',
                                               'order'                 =>
'Student.name',
                                               'limit'                 => '',
                                               'unique'                =>
false,
                                               'dependent'             =>
true,
                                               'finderQuery'           => '',
                                               'deleteQuery'           => ''
                                              ));

My issue is this:
When trying to retrieve all courses a student is signed up for I do
not know what function to call.
I have tried the following:
$this->User->Course->findAllByStudent($this->Session->read('id'),
array('Course.id', 'Course.name'))
But cakePHP generates a not pleasant SQL query:
SELECT `Course`.`id`, `Course`.`name` FROM `courses` AS `Course` LEFT
JOIN `users` AS `Teacher` ON (`Course`.`teacher` = `Teacher`.`id`)
WHERE `Course`.`student` = 24

As you can see cakePHP uses the Teacher relation instead of the
Student relation.
There must be something I'm missing here.

And by the way: I am aware that it is possible to simply retrieve all
the data via $this->User->read(null, $this->Session->read('id')), but
this would cause cakePHP to query a lot of other tables too, some of
which contain an enormous amount of data (well, not yet but soon). I
am afraid that creating an array this big would hurt the performance.
And in the end, I just want the courses, nothing else.

I know this is a big post. So thank you very much for at least
reading, I would be delighted if somebody had a hint or even an answer
to my question.


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to