1 cakephp
对不起我的英文...我有两个模型:用户和注意在user.php:
var $hasMany=array('Note'=>array('className'=>'Note',
'foreignKey'=>'user_id',
'dependent'=>'true',
'exclusive'=>'true'
)
);
Run Code Online (Sandbox Code Playgroud)
在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'));
}
}
Run Code Online (Sandbox Code Playgroud)
但是当我删除用户时,与用户关联的笔记不会被删除!
怎么了????
它应该是:
// 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'
)
);
Run Code Online (Sandbox Code Playgroud)
并立即尝试.它也将删除相关数据.请问它是否对你不起作用.
| 归档时间: |
|
| 查看次数: |
3465 次 |
| 最近记录: |