2012-08-08 114 views
1

对不起我的英语...... 我有两个模式:用户和注意 在user.php的:CakePHP的级联删除不工作

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')); 
    } 
} 

但是,当我删除一个用户,与用户相关的笔记不会被消除!

什么是错?

+0

此代码看起来不错。您的关联和您的delete()方法调用看起来都非常完美,可以处理级联删除关联数据。 – Predominant 2012-08-08 23:46:43

回答

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

现在尝试。它也会删除关联的数据。请问如果它不适合你。

+0

谢谢!错误是'dependent'=>'true', Arun Jain说,它必须是'dependent'=> true,没有单引号! – user1585644 2012-08-09 17:19:14

+0

如果这个答案对你有帮助,那么请把它标记为答案。谢谢 – 2012-08-10 04:11:10