我如何根据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
        (
            ...

"讲师"one_answers"课程"之间存在许多一段关系的关系。如何根据每个"讲师"所属的"课程"过滤结果?我尝试以下没有成功:

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

如果您试图根据针对子项目的条件限制您的父项目,则可以通过儿童模型而不是主模型进行主查询,然后contain()其余的,或使用MySQL Join-在CakePHP中通过()可用()。

您需要(重新)使用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,并且您的条件将起作用。

相关内容

  • 没有找到相关文章

最新更新