dongzhi4073 2013-11-20 08:09
浏览 101
已采纳

yii:试图获得非对象的属性

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); :

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..

  • 写回答

3条回答 默认 最新

  • douan2478 2013-11-20 08:21
    关注

    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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分