3

In yii, I am creating login module. After successful login I have to retrieve current login users userid and want to insert that userid into another 'logintransLog' table which is having userid as attribute with foreign key relationship. So in yii, how to retrieve current userid and insert it into another table? Please help me.....


I want to insert userid into table 'loginAttempt' if only userid is correct and password is wrong, in order to maintain records of how much time user had tried for login. So how can I implement this? I have actionlogin method as:

public function actionLogin()
{
    $model=new LoginForm;
    $command = Yii::app()->db->createCommand();

    // if it is ajax validation request
    if(isset($_POST['ajax']) && $_POST['ajax']==='login-form')
    {
        echo CActiveForm::validate($model);
        Yii::app()->end();
    }

    // collect user input data
    if(isset($_POST['LoginForm']))
    {
        $model->attributes=$_POST['LoginForm'];
        // validate user input and redirect to the previous page if valid
        if($model->validate() && $model->login())
        {
                $command->insert('trans', array(
                    'id'=>Yii::app()->user->getId(),
                //'Ipaddress'=>Yii::app()->user->getIpaddress(),
                ));
            $this->redirect(Yii::app()->user->returnUrl);
        }

    }
    // display the login form
    $this->render('login',array('model'=>$model));
}      

Please help me

2 Answers 2

13

1st, Easy and right: make model logintransLog table and use:

$log = new logintransLog();
$log->user_id = $user->id;
$log->created = date('Y-m-d H:i:s');
$log->save();

2nd, just fast - use createCommand :

$sql = "insert into logintransLog (user_id, created) values (:user_id, :created)";
$parameters = array(":user_id"=>$user->id, ':created' => date('Y-m-d H:i:s'));
Yii::app()->db->createCommand($sql)->execute($parameters);

// try this, if any validation failed it was count as login attempt
if ($model->validate() && $model->login()) {
        $command->insert('trans', array(
           'id'=>Yii::app()->user->getId(),
         //'Ipaddress'=>Yii::app()->user->getIpaddress(),
        ));
        $this->redirect(Yii::app()->user->returnUrl);
} else {
        $command->insert('loginAttempt', array(
           'username'=>$model->username,
           'Ipaddress'=>$_SERVER['REMOTE_ADDR'],
        ));
}
5
  • Sergey sir,I also made changes as per u had told. but in actioncreate method when username or password is wrong, userid is not get inserted into 'attempt' table. i.e.else part is not getting executed. if ($model->validate()) { if ($model->login()) { $command->insert('trans', array( 'id'=>Yii::app()->user->getId(), //'Ipaddress'=>Yii::app()->user->getIpaddress(), )); $this->redirect(Yii::app()->user->returnUrl); } else { $command->insert('Attempt', array( 'id'=>Yii::app()->User->getId(), )); } Nov 21, 2012 at 7:03
  • Sergey sir,i had again made changes as u did...but still entry is not getting inserted into Loginattempt table when username or password field is wrong...please help me... Nov 21, 2012 at 9:09
  • sir...i am not getting any error but user's id is not getting inserted into Attemt table. Nov 21, 2012 at 11:39
  • check attemp table structure, add die before insert command for check. if you know how to debug - debug. if not add many echo %some text where echo is set%
    – Sergey
    Nov 21, 2012 at 11:52
  • let us continue this discussion in chat Nov 21, 2012 at 11:52
0

there is also another way use $log->insert(); instead of $log->save();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.