PHP MySQL Yii - database reading not writing

2019-07-14 03:22发布

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

标签: php mysql yii lamp
1条回答
做自己的国王
2楼-- · 2019-07-14 04:14

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');
    }       
}
查看更多
登录 后发表回答