Yii多表删除不起作用


Yii multiple table deletion not working

我有两个表:useruserProfile。我想使用Yii活动记录从两个表中删除记录。

以下是我的代码:

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.错误。

这是正确的删除方法吗?或者我哪里错了?

感谢

看看发生了什么,在删除记录后,你再次尝试获取已经被删除的模型的信息,这样你就会得到错误404 the requested page does not exist .

如果您想实现这一点,那么您需要在一个单独的变量中获取要删除的模型的id,并进一步使用该变量来删除另一条记录

public function actionDelete($id) {        
        $model = $this->loadModel($id);
         $user_id = $model->user_id; // after user profile is deleted, the model still hold old information, but this line just makes sure everything would work correct whether Yii version you was using
        $model->delete();
        User::model()->deleteByPK($user_id); // a user has one or more profile, it doesn't need to use deleteAll()
    // 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')); // error 404 from here you should check about the actual url which this action use to redirect
}

顺便说一句,如果你想彻底删除用户,为什么不让你的数据库来做呢?在MySQL中,您可以将外键is从RESTRICTED更新为CASCADE,这意味着一旦您删除了User记录,它也会自动由FK删除依赖记录(UserProfile),那么您不需要像这样手动处理它。

伊迪丝:根据我从你那里得到的大量信息,也许是出了问题。我建议您启用日志并查看SQL实现的日志

启用登录protected''config''main.php

'components'=>array(
'log'=>array(
            'class'=>'CLogRouter',
            'routes'=>array(
                array(
                    'class'=>'CFileLogRoute',
                    'levels'=>'error, warning, trace, info',
//                    'categories'=>'application.*',
                    'categories'=>'system.db.CDbCommand',
                ),
                // uncomment the following to show log messages on web pages
                /*array(
                    'class'=>'CWebLogRoute',
                ),*/
            ),
        ),
)

把这行写在你的行动上

Yii::app()->log->processLogs(null);

刷新页面并打开protected'runtime'application.log以查看发生了什么

这不是deleteAll方法错误。您在此处删除记录$id

 $this->loadModel($id)->delete();

然后你打电话给

$this->loadModel($id);

在负载模型方法中,您有以下代码:

if($model===null)
        throw new CHttpException(404,'The requested page does not exist.');

你已经删除了它,所以它为空,你得到了你的错误。

制作loadmodel($id)->delete,然后制作

$model=User::model()->find('id=:id',array(':id'=>$id));
$model->delete();

这会像你需要的那样工作。