[SOLVED] Chapter 13, Trying to get property of non-object

Everything went well until I made this change (following page 331)

That is, in ProjectController::actionIndex(), simply change this:

$sysMessage = SysMessage::model()->find(array('order'=>'t.update_time DESC',));

to the following:

$sysMessage = SysMessage::getLatest();

Initially there was no problem but I continued with the iteration and made the part about fragment cache applied to Recent Comments on page 332. I tried it out and when I created a new comment and went to the project listing page (to test if the new comment was visible) I got a 500 error. This is the application.log trace:

2011/11/19 18:55:29 [error] [php] Trying to get property of non-object (/path/protected/controllers/ProjectController.php:170)

Stack trace:

#0 /path/YiiRoot/framework/web/filters/CFilterChain.php(134): ProjectController->runAction()

#1 /path/YiiRoot/framework/web/filters/CFilter.php(41): CFilterChain->run()

#2 /path/YiiRoot/framework/web/CController.php(1144): CAccessControlFilter->filter()

#3 /path/YiiRoot/framework/web/filters/CInlineFilter.php(59): ProjectController->filterAccessControl()

#4 /path/YiiRoot/framework/web/filters/CFilterChain.php(131): CInlineFilter->filter()

#5 /path/YiiRoot/framework/web/CController.php(283): CFilterChain->run()

#6 /path/YiiRoot/framework/web/CController.php(257): ProjectController->runActionWithFilters()

#7 /path/YiiRoot/framework/web/CWebApplication.php(277): ProjectController->run()

#8 /path/YiiRoot/framework/web/CWebApplication.php(136): CWebApplication->runController()

#9 /path/YiiRoot/framework/base/CApplication.php(158): CWebApplication->processRequest()

#10 /path/index.php(13): CWebApplication->run()


I tested the query in my database and went well, any ideas?

Trying to fix it, it appears that the function SysMessage::getLatest() returns a string instead a SysMessage object, therefore the Error 500 is shown.

Solved, just making a little change to check whether getLatest() returns an instance of SysMessage or just a string.


		$sysMessage = SysMessage::getLatest();


		if(($sysMessage != null)&&($sysMessage instanceof SysMessage)){

			$message = $sysMessage->message;

		}else if($sysMessage != null){

			$message = $sysMessage;

		}else {

			$message = null;


I also changed SysMessageTest::testGetLatest() to this

	public function testGetLatest() {

		$message = SysMessage::getLatest();

		$this->assertTrue(($message instanceof SysMessage)||(is_string($message)));


Hi, i have "Trying to get property on non-object" case too… I want to ask about an error in login action. Let say that i have an application. Those are my code…



class EWebUser extends CWebUser{

    protected $_model;


   protected function loadUser()


        if ( $this->_model === null ) {

                $this->_model = StaffDb::model()->findByPk($this->id);


        return $this->_model;


    function getLevel()



        if ($user->kdstpeg == 02){

        if(substr($user->kdorg,1,4) == '0000') $level=1;

        else if (substr($user->kdorg,2,3) == '000') $level=2;

            return $level;


        return 100;





 * 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 = MUser::model()->find('LOWER(username)=?', array($username));



			else if ($user->pwd!=$this->password)

			$this->errorCode = self::ERROR_PASSWORD_INVALID;



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

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

					$this->errorCode = self::ERROR_NONE;


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


	 public function getId()


		return $this->_id;



and StaffDb.php (one of my model)

public static function model($className=__CLASS__)


		return parent::model($className);



	 * @return string the associated database table name


	public function tableName()


		return 'staffdb';



	 * @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('oldStaffCode', 'required'),

			... array('kdstpeg', 'max'=>2),


			array('...', 'safe'),

			// The following rule is used by search().

			// Please remove those attributes that should not be searched.

			array('oldStaffCode, kdstpeg, ..., 'safe', 'on'=>'search'),



        public function primaryKey()


          return 'oldStaffCode';


When i’m trying to access this application, the error “Trying to get property of non-object” is shown. Then, i put this code on EWebUser.php

print_r ($user);

in order to see what kind of $user’s view. But it didn’t have any effect. :(

My question is, did I miss something in that codes? Because in my opinion, all of objects are complete. I hope there’s a suggestion to solve this problem. Thanks in advance. :D


Hellow guys

i’m new with yii

i try put this code in my view.php


echo $S->nama;

there’s an error message:

"Trying to get property of non-object"

thanks !

i want to echo my user’s “name” in this case

sorry for my bad english