yii : 试图获取非对象的属性 [英] yii : Trying to get property of non-object
问题描述
好吧,我发现了这个,但我看不出与我的错误的关系.. 我是新手——好吧,我知道这个借口是不可接受的,但在我在这里提问之前,我会先尝试找到解决方案.我希望你能帮助我.
Well, I found this, but i can't see the relation with my error.. I am newbie--well I know this excuse is unacceptable but i try to find the solution first before i ask it here. I hope you can help me.
所以我有这个代码,我把这个代码放在视图中:
so I have this code, i put this code in views :
foreach ($budget as $budget):
if (array_key_exists('year', $_GET)) {
$criteria = new CDbCriteria;
$criteria->condition = 'year = ' . $_GET['year'];
$criteria->addCondition('mapping_id = '. $budget->id);
$yearBudget = YearlyBudget::model()->find($criteria);
} else {
$yearBudget = new YearlyBudget;
}
endforeach;
然后在那个代码之后,我只是回应这个:
and then after that code, i simply echo this :
echo $yearBudget->budget;
然后我得到了非对象错误.我只是不明白并停留在这段代码中.请帮忙,谢谢.
then i got non-object error. I just don't understand and stuck in this code. Please help, and thank you.
print_r($yearBudget);:
print_r($yearBudget); :
YearlyBudget Object(
[_new:CActiveRecord:private] =>
[_attributes:CActiveRecord:private] => Array
(
[id] => 1
[mapping_id] => 1
[year] => 2012
[budget] => 2000000
[balance] => 2000000
[created] => 2013-11-20 10:16:29
[updated] =>
)
[_related:CActiveRecord:private] => Array
(
)
[_c:CActiveRecord:private] =>
[_pk:CActiveRecord:private] => 1
[_alias:CActiveRecord:private] => t
[_errors:CModel:private] => Array
(
)
[_validators:CModel:private] =>
[_scenario:CModel:private] => update
[_e:CComponent:private] =>
[_m:CComponent:private] =>
)
啊,对不起,如果我的英语不好..
Ah, sorry if my english is bad..
推荐答案
在访问对象的属性之前检查对象是否为空.
Check whether the object is empty or not before you access it's attribute.
if($yearBudget){
echo $yearBudget->budget;
}else{
echo "No Yearly Budget with given criteria";
}
如果没有得到想要的结果,请仔细检查条件
If you are not getting desired result, check the conditions carefully
$criteria->condition = 'year = ' . $_GET['year'];
$criteria->addCondition('mapping_id = '. $budget->id);
这篇关于yii : 试图获取非对象的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!