Author Topic: How to login using database in YII framework  (Read 19474 times)

Savera

  • Full Member
  • ***
  • Posts: 159
How to login using database in YII framework
« 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.

phpdeveloper

  • Newbie
  • *
  • Posts: 39
Re: How to login using database in YII framework
« Reply #1 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 .

girish baghel

  • Newbie
  • *
  • Posts: 2
Re: How to login using database in YII framework
« Reply #2 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,
      ),





rahul

  • Newbie
  • *
  • Posts: 16
Re: How to login using database in YII framework
« Reply #3 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

phpdeveloper

  • Newbie
  • *
  • Posts: 39
Re: How to login using database in YII framework
« Reply #4 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 .
« Last Edit: September 14, 2012, 06:16:37 AM by phpdeveloper »

Kumail

  • Newbie
  • *
  • Posts: 7
Re: How to login using database in YII framework
« Reply #5 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

 

X

Do you know?

By participating in our community you can get free Top Level Domain for lifetime.
Know more..
Join Now!