PHP MySQL Yii-无法读取数据库

问题描述:

我的本​​地灯泡堆上有一个正在工作的Yii应用程序.现在,当我将应用程序放置在灯泡服务器上时,该应用程序将读取数据库并运行,但是该应用程序未成功写入数据库.我没有错误日志.有什么想法吗?

I have a working Yii app on my local lamp stack. Now when I put the app on a lamp server the app reads the db and runs, but the app isn't successfully writing to the db. I'm getting no errors logs. Any thoughts?

这是我更新数据库的方式:

Here's how I'm updating the db:

public function actionIndex()
{
    if ($_GET["yep"] == "") {
      pd_error("You are not logged in!");
    }
    list($uid, $domain) = preg_split("/@/",$_GET["yep"],2);
    $model=$this->loadModel($uid);
    $this->redirect($model->URL."?".$model->Unique_ID);     
}

public function loadModel($uid)
{
    $model=People::model()->findByPk($uid);
    $model->Login_Count++;
    $model->Last_Logged=date('Y-m-d H:i:s');
    if ($model->validate()) {
         $model->save();
    } else { 
        $this->render('notice');
    }
    return $model;
}

奇怪的是,即使数据库没有更新Login_Count和Last_Logged,用户仍然会重定向到其url,因此sql必须有效,因为通知页面永远不会加载.有什么想法吗?

The weird thing is, even when the db doesn't update the Login_Count and Last_Logged the user still gets redirected to their url, so the sql must be valid because the notice page never loads. Any thoughts?

问题最终是由于mysql服务器将autocommit设置为false.要在应用程序级别覆盖此设置,请在config/main.php db数组中添加以下行:

The problem ended up being that the mysql server had autocommit set to false. To override this at the app level add the following line to the config/main.php db array:

'db'=>array(
    ...
    'initSQLs'=>array('SET AUTOCOMMIT=1',),
    ...
);

Yii:在关闭自动提交的情况下使用活动记录mysql服务器

notice页面的呈现不会停止您的重定向.它可能已呈现,但由于重定向而无法显示.尝试重构您的代码.

The rendering of notice page doesn't stop your redirect. It might be rendered, but you won't be able to see it because of redirect. Try to refactor your code.

  • 您要两次验证模型,由于没有来自应用程序用户的数据,验证可能会被跳过.
  • 您无需检查是否实际找到了People模型.
  • 有一种CWebUser::afterLogin方法,您可以重写此方法(更新登录计数和上次登录日期)
  • You're validating your model twice and the validation probably might be skipped since there's no data coming from App user.
  • You don't check if People model actually found.
  • There is CWebUser::afterLogin method which you can override to do this kind of stuff (update login count and last login date)

也许这种方式(快速修复)将起作用:

Maybe this way (quick fix) will work:

function actionIndex()
{
    if ($_GET["yep"] == "") {
      pd_error("You are not logged in!");
    }
    list($uid, $domain) = preg_split("/@/",$_GET["yep"],2);
    if (null === ($model=People::model()->findByPk($uid))
        throw new CHttpException(404);
    $model->Login_Count++;
    $model->Last_Logged=date('Y-m-d H:i:s');
    if ($model->save()) {
         $this->redirect($model->URL."?".$model->Unique_ID);
    } else {
        // echo CHtml::errorSummary($model)
        $this->render('notice');
    }       
}