Yii Framework Forum: [SOLVED] Chapter 13, Trying to get property of non-object - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

[SOLVED] Chapter 13, Trying to get property of non-object $sysMessage->message error

#1 User is offline   jacksmirk 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 14
  • Joined: 01-November 11
  • Location:Seville, Spain

Posted 19 November 2011 - 01:14 PM

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()
REQUEST_URI=/path/project


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

#2 User is offline   jacksmirk 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 14
  • Joined: 01-November 11
  • Location:Seville, Spain

Posted 21 November 2011 - 06:23 PM

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

#3 User is offline   jacksmirk 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 14
  • Joined: 01-November 11
  • Location:Seville, Spain

Posted 21 November 2011 - 06:41 PM

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

ProjectController::actionIndex()
		$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)));
	}

0

#4 User is offline   mbaksri 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 69
  • Joined: 15-July 11

Posted 15 January 2013 - 01:58 AM

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..
EWebUser.php
<?php
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()
    {
        $user=$this->loadUser();
        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.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 = MUser::model()->find('LOWER(username)=?', array($username));
                if($user===null)
			$this->errorCode=self::ERROR_USERNAME_INVALID;
			else if ($user->pwd!=$this->password)
			$this->errorCode = self::ERROR_PASSWORD_INVALID;
                else
                {
					$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
0

#5 User is offline   Donald Duck Tech 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 01-March 13

Posted 01 March 2013 - 07:30 AM

:rolleyes:
0

#6 User is offline   lewisAS 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 14-March 14

Posted 01 April 2014 - 11:50 PM

Hellow guys
i'm new with yii
i try put this code in my view.php

$S=User::model()->findByPk($model->user_id);
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
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users