henqi
(Hengkhee)
September 8, 2009, 10:46am
1
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.
sluderitz
(Sluderitz)
September 8, 2009, 10:55am
2
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.
henqi
(Hengkhee)
September 8, 2009, 10:58am
3
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.
henqi
(Hengkhee)
September 8, 2009, 11:03am
4
sluderitz:
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?
Ismael
(Shalanga)
September 8, 2009, 11:34am
5
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.
pestaa
(Pestaa)
September 8, 2009, 12:12pm
6
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.
henqi
(Hengkhee)
September 9, 2009, 2:37am
7
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
[sql]
"SELECT lastname FROM Customer WHERE postcode='$postcode' LIMIT 1"
[/sql]
then the problem solved… I am sure I am missing something with the ActiveRecord…