我有两张桌子:user
和userProfile
。我想使用 Yii Active Record 从两个表中删除记录。
以下是我的代码:
public function actionDelete($id) {
$this->loadModel($id)->delete();
$model = $this->loadModel($id);
User::model()->deleteAll('user_id=:id', array(':id' => $model->user_id));
// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
if (!isset($_GET['ajax']))
$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
}
以下是两个模型之间的关系:
public function relations() {
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'userProfiles' => array(self::HAS_MANY, 'UserProfile', 'user_id'),
);
}
public function relations() {
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'user' => array(self::BELONGS_TO, 'User', 'user_id'),
);
}
首先,我从 userProfile 表中删除记录(工作正常),然后user_id
从该模型中获取记录并将其传递给 deleteAll 方法,我试图从user
表中删除记录,但它返回error 404 the requested page does not exist.
错误。
它是正确的删除方法吗?或者我哪里出错了?
谢谢