CakePHP SQL Error


CakePHP SQL Error

我试图在数据库中清除记录,尽管当我进行查询和关联时,它似乎不想正确地执行。我得到了以下错误,我很困惑为什么会发生这种情况:

SQL Error: 1054: Unknown column 'GuardiansStudents.student_id' in 'where clause'

之后显示的查询如下:

Query: SELECT `User`.`id`, `Guardian`.`id` 
       FROM `guardians` AS `Guardian` 
       LEFT JOIN `users` AS `User` ON (`Guardian`.`user_id` = `User`.`id`) 
       WHERE `GuardiansStudents`.`student_id` IS NULL 
       AND `User`.`active` = 1 AND `User`.`changeapprovalneeded` = 0    

我在卫报模型中也有以下关联,不确定我是否这样做是正确的,可能这就是错误发生的地方:

class Guardian extends AppModel {
    var $name = 'Guardian';
    //The Associations below have been created with all possible keys,
    // those that are not needed can be removed
    var $belongsTo = array(
       'User' => array(
           'className' => 'User',
           'foreignKey' => 'user_id',
           'conditions' => '',
           'fields' => '',
           'order' => ''
        ),
    );
    var $hasAndBelongsToMany = array(
        'Student' => array(
            'className' => 'Student',
            'joinTable' => 'guardians_students',
            'foreignKey' => 'guardian_id',
            'associationForeignKey' => 'student_id',
            'unique' => true,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'finderQuery' => '',
            'deleteQuery' => '',
            'insertQuery' => ''
        )
    );

这是执行清除函数的代码:

function manager_purgebygrade() {
    ini_set('max_execution_time','120');
    $this->layout = "manager";
    $this->User->recursive = 0;
    $grades = $this->User->Student->Grade->getDropDownList();
    $this->set(compact('grades'));
    //debug($this->data);
    if(!empty($this->data['User']['grade_id']))
    {
        //$this->User->bindModel(array())
        $users = $this->User->find(
            'list',
            array(
                'fields' => array(
                    'User.id'
                 ),
                 'conditions' => array(
                     'Student.grade_id' => $this->data['User']['grade_id']
                 ),
                 'recursive' => 0
             )
         );
        //debug($users);
        $this->User->deleteAll(array('User.id' => $users), true);
        $this->User->Guardian->bindModel(
             array('hasOne' => array('GuardiansStudents')));
        $guardianswithnostudents = $this->Guardian->deleteGuardiansWithNoStudent();
        $guardians = $this->User->Guardian->find(
            'list',
            array(
                'fields' => array(
                    'User.id',
                    'User.id'
                 ),
                 'conditions' => array(
                     'GuardiansStudents.student_id' => null,
                     'User.active' => 1,
                     'User.changeapprovalneeded' => 0
                 ),
                 'recursive' => 1
            )
        );
        $this->User->deleteAll(array('User.id' => $guardians), true);
        $this->set(compact('users','guardians','guardianswithnostudents'));
    }
}

希望有人能给我指出正确的方向,我将非常感激:)。

where子句:

WHERE `GuardiansStudents`.`student_id` IS NULL 

使用from子句或join子句中没有出现的表GuardiansStudents

模型是GuardiansStudent,不是GuardiansStudents。