首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >HABTM可包含的CakePHP

HABTM可包含的CakePHP
EN

Stack Overflow用户
提问于 2012-12-11 18:44:54
回答 1查看 3.4K关注 0票数 2

为了解释我遇到的问题,我会举个例子。假设我正在建立一个系统,学生可以注册一个或多个课后课程,但学校官员必须批准注册才能使其有效。所以我有这些模型:

  • 课程(属于“教师”,hasAndBelongsToMany“学生”)
  • 学生(hasAndBelongsToMany“课程”)
  • 教师(hasMany“课程”)

现在,假设我想看一张九年级学生所有未经批准的注册名单。这很简单:

代码语言:javascript
复制
$this->request->data = $this->Student->CoursesStudent->find('all', array(
    'conditions' => array(
        'CoursesStudent.is_approved' => null,
        'Student.grade' => 9
    )
));

我正在苦苦挣扎的是,我也在调取老师的数据(不仅仅是他们的身份证)。我试过这个:

代码语言:javascript
复制
$this->request->data = $this->Student->CoursesStudent->find('all', array(
    'conditions' => array(
        'CoursesStudent.is_approved' => null,
        'Student.grade' => 9
    ),
    'contain' => array(
        'CoursesStudent',
        'Course' => array(
            'Teacher' => array(
                'fields' => array(
                    'Teacher.id',
                    'Teacher.name'
                )
            )
        ),
        'Student'
    )
));

但是它对返回的数组没有任何影响。为了让您轻松地将其抛到要使用的现有CakePHP项目中,这里是数据库模式和数据,这里是您可以粘贴到现有CakePHP项目的随机操作中的内容。这就是我想要得到的(注意老师的数据在那里):

代码语言:javascript
复制
Array
(
    [0] => Array
        (
            [CoursesStudent] => Array
                (
                    [id] => 1
                    [course_id] => 1
                    [student_id] => 1
                    [is_approved] => 
                    [created] => 2012-12-11 00:00:00
                    [modified] => 2012-12-11 00:00:00
                )

            [Course] => Array
                (
                    [id] => 1
                    [teacher_id] => 1
                    [title] => Introduction to Improvisation
                    [start_date] => 2012-12-17
                    [end_date] => 2012-12-21
                    [created] => 2012-12-11 00:00:00
                    [modified] => 2012-12-11 00:00:00
                    [Teacher] => Array
                        (
                            [id] => 1
                            [first_name] => John
                            [last_name] => Doe
                            [created] => 2012-12-11 00:00:00
                            [modified] => 2012-12-11 00:00:00
                        )

                )

            [Student] => Array
                (
                    [id] => 1
                    [first_name] => Bill
                    [last_name] => Brown
                    [grade] => 9
                    [created] => 2012-12-11 00:00:00
                    [modified] => 2012-12-11 00:00:00
                )

        )

)

谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-12-11 21:23:00

在使用$recursive之前,必须将contain()设置为-1

此外,确保您的模型被设置为使用可包含的行为。在这种情况下,因为'Course‘也包含了一些东西,所以它也需要使用可控制的行为。

(您可以考虑将下面的$actsAs设置在您的AppModel中,以便使每个模型都可以使用。)

代码语言:javascript
复制
//in your CoursesStudent model (and in your Course model)
public $actsAs = array('Containable');

下面是您的发现,但首先设置递归:

代码语言:javascript
复制
$this->Student->CoursesStudent->recursive = -1;
$this->request->data = $this->Student->CoursesStudent->find('all', array(
    'conditions' => array(
        'CoursesStudent.is_approved' => null,
        'Student.grade' => 9
    ),
    'contain' => array(
        'CoursesStudent',
        'Course' => array(
            'Teacher' => array(
                'fields' => array(
                    'Teacher.id',
                    'Teacher.name'
                )
            )
        ),
        'Student'
    )
));
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13826474

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档