如何根据CakePHP中的子结果限制查询结果? [英] How do I restrict query results based on sub-results in CakePHP?

查看:219
本文介绍了如何根据CakePHP中的子结果限制查询结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在CakePHP中,如果我执行以下find命令

In CakePHP if I run the following find command

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

教师和课程之间的关系如何根据每个教师属于的课程过滤结果?我尝试以下没有成功:

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)
));


推荐答案

尝试根据针对子项的条件限制父项,您可以通过子模型而不是主模型执行主查询,而 contains()其余的,或者使用MySQL连接 - 通过 joins()

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

这篇关于如何根据CakePHP中的子结果限制查询结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆