CakePHP删除级联不起作用

时间:2012-08-08 18:57:32

标签: cakephp

对不起我的英文... 我有两个模型:用户和注意 在user.php中:

var $hasMany=array('Note'=>array('className'=>'Note',
                                 'foreignKey'=>'user_id',
                                 'dependent'=>'true',
                                 'exclusive'=>'true'
                                 )
                    );

在users_controller.php中:

function delete($id = null) {
    if (!$id) {
        $this->Session->setFlash(__('Invalid id for User', true));
        $this->redirect(array('action'=>'index'));
    }
    if ($this->User->delete($id,true)) {
        $this->Session->setFlash(__('User deleted', true));
        $this->redirect(array('action'=>'index'));
    }
}

但是当我删除用户时,与用户关联的备注不会消除!!!

有什么问题????

1 个答案:

答案 0 :(得分:6)

应该是:

// In your User Model
var $hasMany=array('Note'=>array('className'=>'Note',
                                 'foreignKey'=>'user_id',
                                 'dependent'=>true, // true without single quote
                                 'exclusive'=>true
                                )
                );

//In your Note Model
var $belongsTo = array('User'=>array('className'=>'User',
                                     'foreignKey'=>'user_id'
                             )
                );

现在试试。它也将删除相关数据。请问它是否对你不起作用。

相关问题