Yii Framework Forum: PHP Error : Trying to get property of non-object - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

PHP Error : Trying to get property of non-object Rate Topic: -----

#1 User is offline   henqi 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 62
  • Joined: 10-March 09

Posted 08 September 2009 - 05:46 AM

I don't understand how this happen (I want to retrieve single sample customer from particular postcode):
00114:     public function GetCustomerName($state) {
00115:         $criteria = new CDbCriteria;
00116:         $criteria->select = 'lastname';
00117:         $criteria->condition = 'postcode=:xx';
00118:         $criteria->params = array(':xx'=>$postcode);
00119:         $criteria->limit = 1;
00120:         $tmpModel = Customer::model()->find($criteria);
00121: 
00122:         return $tmpModel->lastname;
00123:     }


But Yii through PHP Error:

Description
Trying to get property of non-object, at line 122.


I using find() (not findAll) which should only return 1 ActiveRecord object, but when I
print_r($tmpModel, true)
to yii log, found there is an array of Custommer Active Record, sound like the
$criteria->limit


not working.. can somebody please advice?

Thanks.
0

#2 User is offline   sluderitz 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 105
  • Joined: 26-February 09
  • Location:Germany

Posted 08 September 2009 - 05:55 AM

You should always check if a model was found before accessing a property:
if($tmpModel) {
    return $tmpModel->lastname;
} else {
    return null;
}

Your function has the parameter $state, the variable you are using later on is called $postcode though, so nothing can be found. Please check.
0

#3 User is offline   henqi 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 62
  • Joined: 10-March 09

Posted 08 September 2009 - 05:58 AM

I use Yii DAO to solve this problem.

public function GetCustomerName_DAO($postcode) {
	$command=Yii::app()->db->createCommand(
			"SELECT lastname FROM Customer WHERE postcode=:PARA LIMIT 1");
        $command->bindParam(":PARA", $postcode, PDO::PARAM_INT);
        $dataReader = $command->query();
        $row = $dataReader->read();
        $dataReader->close();

	return $row['lastname'];
}


But I need to understand what is my problem about this..

Thanks.
0

#4 User is offline   henqi 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 62
  • Joined: 10-March 09

Posted 08 September 2009 - 06:03 AM

View Postsluderitz, on 08 September 2009 - 05:55 AM, said:

You should always check if a model was found before accessing a property:
if($tmpModel) {
    return $tmpModel->lastname;
} else {
    return null;
}

Your function has the parameter $state, the variable you are using later on is called $postcode though, so nothing can be found. Please check.


Sorry, my typo mistake (this post topic)... the variable passed should be $postcode, not $state.

Yes, you are right, I need to check what was found everytime using ActiveRecord. For this case and my problem is, the array of ActiveRecrord is found, I need to has only 1 ActiveRecord record, not array.

I use only find(), not findAll(), and I set limit=1, but still return array of records.. :(

Am I miss anythings on the code?
0

#5 User is offline   Ismael 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 163
  • Joined: 02-June 09
  • Location:Brazil

Posted 08 September 2009 - 06:34 AM

Use find() to return a object and not findAll() with limit = 1.

Look in the source code and you'll see that find() internally use limit = 1.

;D
0

#6 User is offline   pestaa 

  • past Yii dev member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 705
  • Joined: 07-May 09
  • Location:Hungary

Posted 08 September 2009 - 07:12 AM

If you use find() you don't have to explicitly set criteria limit -- that's done internally as well.

If you share your logs, we could help you more easily. As sluderitz mentioned, you used an unknown variable ($postcode), and passed an unnecessary parameter ($state), this might also cause some headache for the interpreter.
0

#7 User is offline   henqi 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 62
  • Joined: 10-March 09

Posted 08 September 2009 - 09:37 PM

Sorry to you all, here is the original code (I typo error in the first post of this title), the $state is non exist, it is actually $postcode.

The find() return 1 object as expected. (my problem in the previous post, course I has a for loop to call this function)

00114:     public function GetCustomerName($postcode) {
00115:         $criteria = new CDbCriteria;
00116:         $criteria->select = 'lastname';
00117:         $criteria->condition = 'postcode=:xx';
00118:         $criteria->params = array(':xx'=>$postcode);
00119:         $criteria->limit = 1;
00120:         $tmpModel = Customer::model()->find($criteria);
00121:         Yii::log(print_r($tmpModels, true), 'info');
00122:         return $tmpModel->lastname;
00123:     }



The log too long, I paste the beginning and end (skip table definition) of the dump:

2009/09/09 10:29:26 [info] [application] Customer Object
(
    [_md:private] => CActiveRecordMetaData Object
        (
            [tableSchema] => CMysqlTableSchema Object
                (
                    [schemaName] =>
                    [name] => Customer
                    [rawName] => `Customer`
                    [primaryKey] => id
                    [sequenceName] =>


        )

    .....
    .....
    ..... 
    .....

    [_new:private] =>
    [_attributes:private] => Array
        (
            [lastname] => Smith
        )

    [_related:private] => Array
        (
        )

    [_c:private] =>
    [_errors:private] => Array
        (
        )

    [_va:private] =>
    [_se:private] =>
    [_e:private] =>
    [_m:private] =>
)



The Customer model is generated by Yii shell, with no additional added variables., just add some private function.

From the log. this is the ActiveRecord object I needed, and I found the data I want: "Smith",
but when I want to access data by

     return $tmpModel->lastname;


Yii show me "Trying to get property of non-object" error... I use DAO by this query
    "SELECT lastname FROM Customer WHERE postcode='$postcode' LIMIT 1"

then the problem solved.. I am sure I am missing something with the ActiveRecord... :(
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

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