0

In CakePHP if I run the following find command

$this->Instructor->find('all');

the following is returned:

Array
(
    [0] => Array
        (
            [Instructor] => Array
                (
                    [id] => 1
                    [user_id] => 3
                    [bio] => A billionaire playboy, industrialist and ingenious engineer, Tony Stark suffers a severe chest injury during a kidnapping in which his captors attempt to force him to build a weapon of mass destruction. He instead creates a powered suit of armor to save his life and escape captivity. He later uses the suit to protect the world as Iron Man.
                )

            [User] => Array
                (
                    [id] => 3
                    [first_name] => Tony
                    [last_name] => Stark
                    [asurite_user] => tstark
                    [asurite_id] => 
                    [password] => 
                    [email] => tstark@example.com
                    [created] => 2012-10-30 09:57:36
                    [modified] => 2012-10-30 09:57:36
                )

            [Course] => Array
                (
                    [0] => Array
                        (
                            [id] => 1
                            [slug] => beatles
                            [sln] => AAA001
                            [course_number] => 
                            [title] => The Beatles
                            [description] => This is a class about the Beatles.  If this were a real description, more information would be listed here.
                            [state] => 
                        )

                    [1] => Array
                        (
                            [id] => 2
                            [slug] => elvis
                            [sln] => AAA002
                            [course_number] => 
                            [title] => Elvis: The King of Rock
                            [description] => All about the king of rock and roll, Elvis Presley.
                            [state] => 
                        )

                )

        )

    [1] => Array
        (
            ...

There is a many-to-many relationship between "Instructor" and "Course". How can I filter the results based on the "Course"s each "Instructor" belongs to? I tried the following without success:

$instructors = $this->Instructor->find('all', array(
    'conditions' => array('Course.id' = 2)
));
PHLAK
  • 22,023
  • 18
  • 49
  • 52

2 Answers2

1

If you're trying to restrict your parent item based on conditions against your child item, you can either do your main query through the child model instead of the main model, and contain() the rest, or use MySQL Joins - available in CakePHP via joins().

Dave
  • 28,833
  • 23
  • 113
  • 183
  • Adding a "joins" to the find options is exactly what I needed to do. It was actually very similar to the example they give at the bottom. – PHLAK Nov 01 '12 at 22:46
0

You need to (re)bind the associations for your Instructor model with hasOne and use these settings:

'CoursesInstructor' => array(
    'className' => 'CoursesInstructor',
    'foreignKey' => false,
    'conditions' => 'Instructor.id = CoursesInstructor.id'),
'Course' => array(
    'className' => 'Course',
    'foreignKey' => false,
    'conditions' => 'CoursesInstructor.course_id = Course.id');

This will generate the correct SQL with the required joins and your conditions will work.

floriank
  • 25,546
  • 9
  • 42
  • 66
  • That seems counter-intuitive. The hasAndBelongsToMany relationship exists for exactly the purpose I'm describing (Courses can have multiple instructors and instructors can have multiple courses). – PHLAK Oct 31 '12 at 22:47