首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

yii Trying to get property of non-object异常

2013-02-24 
yii Trying to get property of non-object错误??这样的代码,就会出现上面的错误。??? public function get

yii Trying to get property of non-object错误

?

?这样的代码,就会出现上面的错误。
??? public function getCityName($id)
??? {
??? ??? $model = City::model()->findByPk($id);
??? ??? ??? return $model->name;
??? }

一定要做个判断,正确的如下:

??? public function getCityName($id)
??? {
??? ??? $model = City::model()->findByPk($id);
??? ????
??? ??? if($model){
??? ??? ??? return $model->name;
??? ??? }
??? ??? else{
??? ??? ??? return null;
??? ??? }
??? }

?譬如:/**?* Returns the data model based on the primary key given in the GET variable.?* If the data model is not found, an HTTP exception will be raised.?* @param integer the ID of the model to be loaded?*/public function loadModel($id){$model=Member::model()->findByPk($id);if($model===null)throw new CHttpException(404,'抱歉,你请求的页面不存在.');return $model;}

热点排行