2

这是我的基本设置... 家庭 hasMany Students hasMany Enrollment

如果注册为空,有没有办法排除学生?这是我的查找方法。

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

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

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

                            )

                    )
              )
4

2 回答 2

1

加入注册时摆脱加入。改为使其成为内部连接。

$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);
于 2013-09-28T00:34:09.713 回答
1

我不明白当你有 Containable 模型时为什么要使用连接。顺便说一句,我找到了这个解决方案:它有效,但我认为这不是最好的方法。

在您的Family模型中

public $hasMany = array
(
    'Student' => array(
        'conditions' => array('Student.id IN (SELECT DISTINCT student_id FROM enrollments)')
    )
);

或者,如果您愿意,您可以仅在需要时使用

Family->bindModel(array(
    'hasMany' => array (
            'Student' => array(
                'conditions' => array('Student.id IN (SELECT DISTINCT student_id FROM enrollments)')
        )
    )
)
于 2013-10-04T06:50:50.740 回答