YII致命错误:调用非对象的成员函数

时间:2013-12-04 04:24:12

标签: php yii

谁能帮帮我? 我遇到了这段代码。

$billing->bookingHeader->getCodeNumber('BKG');

实际上,$ billing是一种模式,而bookingHeader就是这种关系。

此行生成错误

Fatal error: Call to a member function getCodeNumber() on a non-object

以下是包含结果的组件。

class MonthlyTransactionActiveRecord extends ActiveRecord
{

    public function getCodeNumber($cnConstant = '')
    {
        $arr = array('I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX', 'X', 'XI', 'XII');
        $month = $this->cn_month ? $this->cn_month - 1 : 0;
        return sprintf('%02d/%04d/%s/%s/%02d', $this->hotel_id, $this->cn_ordinal, $cnConstant, $arr[$month], $this->cn_year);
    }
}

我两次调用$billing->bookingHeader->getCodeNumber('BKG');(在视图和控制器中),它在视图中没有错误,但是当我在Controller中调用它时,它给了我错误。

错误导致什么样的触发?如何解决? 谢谢..

1 个答案:

答案 0 :(得分:0)

在调用方法之前检查对象天气是否为空。

if($billing->bookingHeader){
  $billing->bookingHeader->getCodeNumber('BKG');
}else{
  echo "bookingHeader is empty";
}
相关问题