0

次の find コマンドを実行すると、CakePHP で

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

以下が返されます。

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
        (
            ...

「インストラクター」と「コース」の間には多対多の関係があります。各「インストラクター」が属する「コース」に基づいて結果をフィルタリングするにはどうすればよいですか? 私は成功せずに次のことを試しました:

$instructors = $this->Instructor->find('all', array(
    'conditions' => array('Course.id' = 2)
));
4

2 に答える 2

1

子アイテムに対する条件に基づいて親アイテムを制限しようとしている場合は、メイン モデルの代わりに子モデルを介してメイン クエリを実行し、残りを含む ()か、CakePHP で利用可能な MySQL Joins を使用できます。joins()経由。

于 2012-11-01T04:04:16.483 に答える
0

インストラクターモデルの関連付けをhasOneに(再)バインドし、次の設定を使用する必要があります。

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

これにより、必要な結合を含む正しいSQLが生成され、条件が機能します。

于 2012-10-31T22:15:03.177 に答える