Webmaster's Community

Designing and Development => Open Source => Yii PHP Framework Support => Topic started by: Savera on September 11, 2012, 12:27:22 PM

Title: How to login using database in YII framework
Post by: Savera on September 11, 2012, 12:27:22 PM
I am learning YII , they already given us demo webapp . How do I create my own user and then use those values to login to admin panel provided by Yii in their demo  or webapp default setup ?

I am trying to read their documentation but it is not clear, I am looking for something which is easy , straightforward and explained with clarity and keeping in mind that even newbie can understand those documentation.
Title: Re: How to login using database in YII framework
Post by: phpdeveloper on September 13, 2012, 07:22:08 AM
For login With database user and password you have to change useridentity.php authenticate mehtod and configure  your Main.php also .
Title: Re: How to login using database in YII framework
Post by: girish baghel on September 13, 2012, 12:12:28 PM
For login With database have to change useridentity.php  authenticate mehtod and .

change authenticate mehtod  like this.........


public function authenticate() {
        $user = User::model()->findByAttributes(array('username' => $this->username));
        if ($user === null) {
            $this->errorCode = self::ERROR_USERNAME_INVALID;
        } else {
            if ($user->password !== $this->password) {
                $this->errorCode = self::ERROR_PASSWORD_INVALID;
            } else {
                $this->_id = $user->id;
                         
                $this->errorCode = self::ERROR_NONE;
            }
        }
        return !$this->errorCode;
    }

    public function getId() {
        return $this->_id;
    }



and change your main.php in config like this.........


// application components
   'components'=>array(
      'user'=>array(
         // enable cookie-based authentication
         'allowAutoLogin'=>true,
      ),




Title: Re: How to login using database in YII framework
Post by: rahul on September 14, 2012, 06:09:43 AM
Where i have to change if i want to login with email and password apart from user name please give the code for that Thanks in Advance
Title: Re: How to login using database in YII framework
Post by: phpdeveloper on September 14, 2012, 06:14:47 AM
Hey if you want to login with email you can simply channge
$user = User::model()->findByAttributes(array('username' => $this->username)); to
$user = User::model()->findByAttributes(array('email' => $this->username)); or whatever column name you have in you database .
Title: Re: How to login using database in YII framework
Post by: Kumail on September 14, 2012, 12:41:50 PM
i m also agree with php developer this is nice trick to login with username or email also.
that is what i am looking for .
Thank you Very much