-1

I have come across many posts for 'trying to get property of non-object', but I'm still unable to figure out my problem. I have a webapp and when I create a form it is giving me this error-

view.php:

$this->breadcrumbs=array(
    'Events'=>array('index'),
$model->ename, //Error at this line
);

controller:

public function actionView($id)
{
$this->render('view',array(
'model'=>$this->loadModel($id),
 ));
 }

public function loadModel($id)
{
$ename= Event::model()->ename;
$model = Event::model()->findByPk($id,$ename);//tried this since I have composite key
if($model===null)
throw new CHttpException(404,'The requested page does not exist.');
return $model;
}
user3496974
  • 145
  • 1
  • 12
  • possible duplicate of [Trying to get property of non-object error in Yii](http://stackoverflow.com/questions/18335513/trying-to-get-property-of-non-object-error-in-yii) – Rich Apr 24 '14 at 10:37
  • you should debug/check (may be using vardump) if $this->loadModel($id) is returning any object or null value. If sometimes it can return null then you should put some constrain on it. – lihsus Apr 24 '14 at 10:37
  • What is the name of your controller and what is the name of your model? – Jurik Apr 24 '14 at 10:42
  • controller name is EventController, and model is Event – user3496974 Apr 24 '14 at 10:45
  • And you are sure that your `$id` is a valid id and you can find this id in your database as well? Because like @lihsus wrote, it looks like that your controller can not find your model with this id. – Jurik Apr 24 '14 at 10:50
  • The problem is bcz I have a composite key for my table. If I drop one column in the PK, its working fine. How to make it work with composite key? – user3496974 Apr 25 '14 at 05:21

2 Answers2

0

I'm not a Yii expert, but in general this means that $model is not an object. In most frameworks you'd use $this->model instead of $model. Could that be the case? So try $this->model->ename?

Looking at How to use model object in Yii Controller and View it seems this is the case.

Community
  • 1
  • 1
Erik Duindam
  • 349
  • 1
  • 8
  • No, that's wrong. When you pass a model via array thru the render function, this model is avaible under `$model` in the view. – Jurik Apr 24 '14 at 10:40
  • OK, then some Yii expert has to answer this or he'd have to var_dump everything. – Erik Duindam Apr 24 '14 at 10:46
  • 1
    Yes, he has to var_dump everything - I guess his `$id` is wrong. So he can not load a model. So `$model` is just `null`. – Jurik Apr 24 '14 at 10:51
0

Depending on the way you used to load the model, the result will be NULL or an empty array when there is no result. find returns NULL, findAll will always return a array, but it will be empty when there is no result.

Looking at your code, the initial loading of the model fails. Most likely due to the fact that the requested database record is non-existing. To check if the model is loaded succesfull you can do this:

if(is_null($model)) { /* Used with find */ }
if(!count($models)) { /* used with findAll */ }
Michiel
  • 2,143
  • 1
  • 21
  • 21