cakephp - 排除 CakePHP 中的 hasMany 模型

标签 cakephp join containable

这是我的基本设置...家庭有很多学生有很多注册

如果注册为空,是否可以排除学生?这是我的查找方法。

    $options = array( 
        'order' => array('Family.family_last_name'),
        'group' => 'Family.id',
        'contain' => array(
            'Student', 'Student.Enrollment'
        ),
        'joins' => array(
            array(
                'table' => 'students',
                'alias' => 'Student',
                'type' => 'left',
                'conditions' => array(
                    'Family.id = Student.family_id'
                )
            ),
            array(
                'table' => 'enrollment',
                'alias' => 'Enrollment',
                'type' => 'left',
                'conditions' => array(
                    'Enrollment.student_id = Student.id'
                )
            )
        ),
        'conditions' => array(
            'Enrollment.status =' => 'withdrawn'
        )
    );

    $enrollment = $this->Family->find( 'all', $options);

这是它返回的数组。如何删除 jack ?

    [Student] => Array
            (
                [0] => Array
                    (
                        [id] => 92
                        [first_name] => Jack
                        [Enrollment] => Array
                            (
                            )

                    )

                [1] => Array
                    (
                        [id] => 93
                        [first_name] => Jill
                        [Enrollment] => Array
                            (
                                [0] => Array
                                    (
                                        [id] => 99
                                        [student_id] => 93
                                        [grade] => 4
                                    )

                            )

                    )
              )

最佳答案

加入注册时删除加入。改为内部连接。

$options = array( 
    'order' => array('Family.family_last_name'),
    'group' => 'Family.id',
    'contain' => array(
        'Student', 'Student.Enrollment'
    ),
    'joins' => array(
        array(
            'table' => 'students',
            'alias' => 'Student',
            'type' => 'left',
            'conditions' => array(
                'Family.id = Student.family_id'
            )
        ),
        array(
            'table' => 'enrollment',
            'alias' => 'Enrollment',
            'type' => 'inner',
            'conditions' => array(
                'Enrollment.student_id = Student.id'
            )
        )
    ),
    'conditions' => array(
        'Enrollment.status =' => 'withdrawn'
    )
);

$enrollment = $this->Family->find( 'all', $options);

关于cakephp - 排除 CakePHP 中的 hasMany 模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19061938/

相关文章:

php - Cakephp - 包含(可包含行为)获取太多

php - cakePHP formHelper postLink 图片

mysql - CakePhp 包含太多查询

CakePHP 包含不适用于递归

ruby-on-rails - 为什么 Ruby on Rails 没有内置标准的用户认证系统?

php - CakePHP 嵌套查询 : select from another select statement

linux - 根据列中的公共(public)值合并两个文件

MySQL:不为第 1 列和第 2 列插入重复项

mysql模式匹配两个不同表中的前5个字符,然后将数据输入到单独的列中

CakePHP - 是否可以通过 read() 方法使用 Containable 行为?