Property "User.demo" is not defined.

Hi everybody,

I just started to learn how to use Yii yesterday and I’m already in love with it. Before I was using CodeIgniter (and I still am).

I have been following the Blog Tutorial (PDF version, if that makes a difference) and all has gone well until half an hour ago. I reached the end of the first milestone and when I tried to log in using demo as username and password, I got the following error:




CException

Description


Property "User.demo" is not defined.

Source File


/Users/aziz/Sites/yii/yii/framework/db/ar/CActiveRecord.php(106)


00094:      */

00095:     public function __get($name)

00096:     {

00097:         if(isset($this->_attributes[$name]))

00098:             return $this->_attributes[$name];

00099:         else if(isset($this->getMetaData()->columns[$name]))

00100:             return null;

00101:         else if(isset($this->_related[$name]))

00102:             return $this->_related[$name];

00103:         else if(isset($this->getMetaData()->relations[$name]))

00104:             return $this->getRelated($name);

00105:         else

00106: return parent::__get($name);

00107:     }

00108: 

00109:     /**

00110:      * PHP setter magic method.

00111:      * This method is overridden so that AR attributes can be accessed like properties.

00112:      * @param string property name

00113:      * @param mixed property value

00114:      */

00115:     public function __set($name,$value)

00116:     {

00117:         if($this->setAttribute($name,$value)===false)

00118:         {


Stack Trace


#0 /Users/aziz/Sites/yii/yii/framework/db/ar/CActiveRecord.php(106): CComponent->__get('demo')

#1 /Users/aziz/Sites/yii/blog/protected/models/User.php(74): CActiveRecord->__get('demo')

#2 /Users/aziz/Sites/yii/blog/protected/components/UserIdentity.php(19): User->validatePassword('demo')

#3 /Users/aziz/Sites/yii/blog/protected/models/LoginForm.php(50): UserIdentity->authenticate()

#4 /Users/aziz/Sites/yii/yii/framework/validators/CInlineValidator.php(39): LoginForm->authenticate('password', Array)

#5 /Users/aziz/Sites/yii/yii/framework/validators/CValidator.php(168): CInlineValidator->validateAttribute(Object(LoginForm), 'password')

#6 /Users/aziz/Sites/yii/yii/framework/base/CModel.php(150): CValidator->validate(Object(LoginForm), NULL)

#7 /Users/aziz/Sites/yii/blog/protected/controllers/SiteController.php(80): CModel->validate()

#8 /Users/aziz/Sites/yii/yii/framework/web/actions/CInlineAction.php(32): SiteController->actionLogin()

#9 /Users/aziz/Sites/yii/yii/framework/web/CController.php(300): CInlineAction->run()

#10 /Users/aziz/Sites/yii/yii/framework/web/CController.php(278): CController->runAction(Object(CInlineAction))

#11 /Users/aziz/Sites/yii/yii/framework/web/CController.php(257): CController->runActionWithFilters(Object(CInlineAction), Array)

#12 /Users/aziz/Sites/yii/yii/framework/web/CWebApplication.php(320): CController->run('login')

#13 /Users/aziz/Sites/yii/yii/framework/web/CWebApplication.php(120): CWebApplication->runController('site/login')

#14 /Users/aziz/Sites/yii/yii/framework/base/CApplication.php(135): CWebApplication->processRequest()

#15 /Users/aziz/Sites/yii/blog/index.php(11): CApplication->run()

#16 {main}


2010-01-23 20:48:23 Apache Yii Framework/1.1.0




I could post some code if you ask me which part of the app you would need, but my UserIdentity class is exactly the same as the one in the demos folder blog app as are the validatePassword and hashPassword actions.

Does anybody have a clue about what could be the problem please?

Hey!

Show us this two files:

#1 /Users/aziz/Sites/yii/blog/protected/models/User.php(74): CActiveRecord->__get(‘demo’)

#2 /Users/aziz/Sites/yii/blog/protected/components/UserIdentity.php(19): User->validatePassword(‘demo’)

Thanks for your answer. Here are the two files:

User.php:




<?php


class User extends CActiveRecord

{

	/**

	 * The followings are the available columns in table '{{user}}':

	 * @var integer $id

	 * @var string $username

	 * @var string $password

	 * @var string $salt

	 * @var string $email

	 * @var string $profile

	 */


	/**

	 * Returns the static model of the specified AR class.

	 * @return CActiveRecord the static model class

	 */

	public static function model($className=__CLASS__)

	{

		return parent::model($className);

	}


	/**

	 * @return string the associated database table name

	 */

	public function tableName()

	{

		return '{{user}}';

	}


	/**

	 * @return array validation rules for model attributes.

	 */

	public function rules()

	{

		// NOTE: you should only define rules for those attributes that

		// will receive user inputs.

		return array(

			array('username, password, salt, email', 'required'),

			array('username, password, salt, email', 'length', 'max'=>128),

			array('profile', 'safe'),

		);

	}


	/**

	 * @return array relational rules.

	 */

	public function relations()

	{

		// NOTE: you may need to adjust the relation name and the related

		// class name for the relations automatically generated below.

		return array(

		);

	}


	/**

	 * @return array customized attribute labels (name=>label)

	 */

	public function attributeLabels()

	{

		return array(

			'id' => 'Id',

			'username' => 'Username',

			'password' => 'Password',

			'salt' => 'Salt',

			'email' => 'Email',

			'profile' => 'Profile',

		);

	}

	

	public function validatePassword($password)

	{

		return $this->hashPassword($password, $this->salt) === $this->$password;

	} // End of validatePassword

	

	public function hashPassword($password, $salt)

	{

		return md5($salt.$password);

	} // End of hashPassword

}



UserIdentity.php:




<?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

{

	private $_id;

	

	public function authenticate()

	{

		$username = strtolower($this->username);

		$user = User::model()->find('LOWER(username)=?', array($username));

		

		if($user === null)

			$this->errorCode=self::ERROR_USERNAME_INVALID;

		else if(!$user->validatePassword($this->password))

			$this->errorCode=self::ERROR_PASSWORD_INVALID;

		else

		{

			$this->_id = $user->id;

			$this->username = $user->username;

			$this->errorCode = self::ERROR_NONE;

		}

		return $this->errorCode == self::ERROR_NONE;

	}

	

	public function getId()

	{

		return $this->_id;

	} // End of getId

}



return $this->hashPassword($password, $this->salt) === $this->$password;

should be

            return &#036;this-&gt;hashPassword(&#036;password, &#036;this-&gt;salt) === &#036;this-&gt;password;

Oh!! Nice catch! What an idiot (talking about me).

Thanks a lot for the help :)