使用YII进行用户身份验证时出错 - 未定义属性“User.title”

I am trying to do database login for a YII app, and I am getting this error. Here is my code:

<?php

/**
 * UserIdentity represents the data needed to identity a user.
 * It contains the authentication method that checks if the provided
 * data can identity the user.
 */
class UserIdentity extends CUserIdentity
{
    /**
     * Authenticates a user.
     * The example implementation makes sure if the username and password
     * are both 'demo'.
     * In practical applications, this should be changed to authenticate
     * against some persistent user identity storage (e.g. database).
     * @return boolean whether authentication succeeds.
     */
    private $_id;
    public function authenticate()
    {
        // $users=array(
        //  // username => password
        //  'username'=>'password',
        // );
        // if(!isset($users[$this->username]))
        //  $this->errorCode=self::ERROR_USERNAME_INVALID;
        // elseif($users[$this->username]!==$this->password)
        //  $this->errorCode=self::ERROR_PASSWORD_INVALID;
        // else
        //  $this->errorCode=self::ERROR_NONE;
        // return !$this->errorCode;

        $record=User::model()->findByAttributes(array('username'=>$this->username));
        print_r($record); 
        echo ($this->password); 
        echo ($record->password); 
         if($record===null)
             $this->errorCode=self::ERROR_USERNAME_INVALID;
         else if($record->password!==$this->password)
             $this->errorCode=self::ERROR_PASSWORD_INVALID;
         else
         {
             $this->_id=$record->id;
             $this->setState('title', $record->title);
             $this->errorCode=self::ERROR_NONE;
         }
         return !$this->errorCode;

    }

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

Here is the stack trace:

enter image description here

As a note, I have tried to remove the $this->setState('title', $record->title); code, but that just caused another error. I have looked at the values of the User, and there is no title, I thought that might be what is causing the error, but like I said, when I remove the call, I get another error:

Fatal error: Call to a member function getId() on a non-object in /home/fredgran/public_html/yii/framework/web/auth/CWebUser.php on line 229

You are not defining title attribute in your database. But, if you want to make a login task, just try this:

else
     {
         $this->_id=$record->id;
         //$this->setState('title', $record->title);
         $this->username = $record->username; //try this code
         $this->errorCode=self::ERROR_NONE;
     }

CMIIW

remove print_r($record); echo ($this->password); echo ($record->password);

and change line 44 to

if(isset($record->title))
      $this->setState('title', $record->title);

error "Call to a member function getId() on a non-object" is because calling Yii::app()->user->id;

Please copy your User model if the above fix is not working

There is no "title" field in database for user table ... you are trying to use that as $record->title so you are getting this error.

The solution is very simple. This part of the code causes it.

 else
 {
     $this->_id=$record->id;
     $this->setState('title', $record->title);
     $this->errorCode=self::ERROR_NONE;
 }

Make sure you have a column called "title" in your user table or whatever the table that your user model refers to.

With this line,

$this->setState('title', $record->title);

you are trying to create a session variable called "title" and assign user record's title column value.

This is a example of how to set a session and retrieve it in yii.

Yii::app()->user->setState('username', $record->username); // define
Yii::app()->user->username // retrieve, can be access anywhere in the system

Read here for more : http://www.yiiframework.com/doc/api/1.1/CWebUser#setState-detail

Hope this helps.

Observe the stack trace in errors (Attached sceen-shot)

CActiveRecord->__get("title")

The meaning of this error is "You are accessing the the property which is not defined in the Model". ie, your User table does not have the column title.

Please print and check the $record object. Also check Typo error to track the error.