1

Excuse me my english... I have two models: User and Note In user.php:

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

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

But when I delete an user, the notes associated with the user are not eliminated!!!

What is wrong????

4

1 回答 1

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

现在试试。它也会删除相关的数据。请询问它是否不适合您。

于 2012-08-09T04:33:54.300 回答