yii Trying to get property of non-object错误

网友投稿 317 2022-08-24

yii Trying to get property of non-object错误

/*******************Title:yii Trying to get property of non-object错误Author:insunBlog:去执行时候因为调用另外一个model 所以可能会出现Trying to get property of non-object

所以在要在调用model之前判断这个model是否存在

这样的代码,就会出现上面的错误。

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; }

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:YII学习笔记
下一篇:如何策划一次火爆的营销活动?(做营销活动有哪些技巧)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~