我安装了yii user module的yii webapp全新安装,但问题是' lastvisit_at'登录时永远不会更新db字段。它始终设置为默认值00:00:00'我正在使用netbeans搜索整个项目寻找这个设置的位置,我认为我看到的功能,但无法让它工作。我是Yii和框架的新手,所以我可能只是忽略了一些东西。
user.php的
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.CConsoleApplication
return ((get_class(Yii::app())=='CConsoleApplication' || (get_class(Yii::app())!='CConsoleApplication' && Yii::app()->getModule('user')->isAdmin()))?array(
array('username', 'length', 'max'=>20, 'min' => 3,'message' => UserModule::t("Incorrect username (length between 3 and 20 characters).")),
array('password', 'length', 'max'=>128, 'min' => 4,'message' => UserModule::t("Incorrect password (minimal length 4 symbols).")),
array('email', 'email'),
array('username', 'unique', 'message' => UserModule::t("This user's name already exists.")),
array('email', 'unique', 'message' => UserModule::t("This user's email address already exists.")),
array('username', 'match', 'pattern' => '/^[A-Za-z0-9_]+$/u','message' => UserModule::t("Incorrect symbols (A-z0-9).")),
array('status', 'in', 'range'=>array(self::STATUS_NOACTIVE,self::STATUS_ACTIVE,self::STATUS_BANNED)),
array('superuser', 'in', 'range'=>array(0,1)),
array('create_at', 'default', 'value' => date('Y-m-d H:i:s'), 'setOnEmpty' => true, 'on' => 'insert'),
array('lastvisit_at', 'default', 'value' => '0000-00-00 00:00:00', 'setOnEmpty' => true, 'on' => 'insert'),
array('username, password, email, superuser, status', 'required'),
array('superuser, status', 'numerical', 'integerOnly'=>true),
array('id, username, password, email, activkey, create_at, lastvisit_at, superuser, status', 'safe', 'on'=>'search'),
):((Yii::app()->user->id==$this->id)?array(
array('username, email', 'required'),
array('username', 'length', 'max'=>20, 'min' => 3,'message' => UserModule::t("Incorrect username (length between 3 and 20 characters).")),
array('email', 'email'),
array('username', 'unique', 'message' => UserModule::t("This user's name already exists.")),
array('username', 'match', 'pattern' => '/^[A-Za-z0-9_]+$/u','message' => UserModule::t("Incorrect symbols (A-z0-9).")),
array('email', 'unique', 'message' => UserModule::t("This user's email address already exists.")),
):array()));
}
LoginController.php
public function actionLogin()
{
if (Yii::app()->user->isGuest) {
$model=new UserLogin;
// collect user input data
if(isset($_POST['UserLogin']))
{
$model->attributes=$_POST['UserLogin'];
// validate user input and redirect to previous page if valid
if($model->validate()) {
$this->lastVisit();
if (Yii::app()->user->returnUrl=='/index.php')
$this->redirect(Yii::app()->controller->module->returnUrl);
else
$this->redirect(Yii::app()->user->returnUrl);
}
}
// display the login form
$this->render('/user/login',array('model'=>$model));
} else
$this->redirect(Yii::app()->controller->module->returnUrl);
}
private function lastVisit() {
$lastVisit = User::model()->notsafe()->findByPk(Yii::app()->user->id);
$lastVisit->lastvisit = time();
$lastVisit->save();
}
答案 0 :(得分:0)
在你的lastVisit()函数中尝试更改此
$lastVisit->lastvisit = time();
到这个
$lastVisit->lastvisit_at = time();
答案 1 :(得分:0)
其实我弄清楚了。当我实现Yii用户模块时,我没有替换登录方法的CMenu url。一旦我换掉它就按预期工作了。