0

我有两张桌子:useruserProfile。我想使用 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.错误。

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

谢谢

4

3 回答 3

1

查看发生了什么是在删除记录后您再次尝试获取已被删除的模型的信息,因此您收到错误错误404 the requested page does not exist .

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

于 2013-08-13T09:51:13.273 回答
1
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 中,可以更新外键 from RESTRICTEDto CASCADE,这意味着一旦删除了 User 记录,FK 也会自动删除依赖记录(UserProfile),这样就不需要手动处理了。

编辑:根据我从你那里得到的大量信息,也许有问题。建议你开启log看看SQL实现的log

启用登录 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以查看发生了什么

于 2013-08-13T08:31:46.600 回答
0

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

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

然后你打电话

$this->loadModel($id);

在 loadmodel 方法中,您有以下代码:

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

这将像您需要的那样工作。

于 2013-08-13T07:56:15.257 回答