PHP MySQL Yii - database reading not writing

跟風遠走 提交于 2020-01-05 03:28:41

问题


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;
}

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?

Update + Solution

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: using active record with autocommit off on mysql server


回答1:


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.

  • 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');
    }       
}


来源:https://stackoverflow.com/questions/14288327/php-mysql-yii-database-reading-not-writing

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!