I Have This Error Trying To Get Property Of Non-Object

Hi everybody,

I have two tables: "demande" and "statut". the primary key of "statut" migrates in "demande". I generated the models, controllers and views with gii. I inserted data in theses tables.

But when I want to view the recorded information, it sends me an error "[color="#FF0000"]Trying to get property of non-object[/color]".

Here one view called "view"

<?php SysImmo::getNavigation();?><?php

/* @var $this DemandeController */

/* @var $model Demande */

$this->breadcrumbs=array(

'Demandes'=&gt;array('index'),


&#036;model-&gt;id_demande,

);

$this->menu=SysImmo::getMenuPorteletView($model->id_demande);

?>

<?php $this->widget(‘zii.widgets.CDetailView’, array(

'data'=&gt;&#036;model,


'attributes'=&gt;array(


	'libelle',


	'date',





	'motif',


  [color=&quot;#FF0000&quot;]  array(


        'label'=&gt;&#036;model-&gt;getAttributeLabel('statut'),


        'value'=&gt;CHtml::encode(StatutDemande::model()-&gt;findByPk(&#036;model-&gt;statut)-&gt;nom),


    ),[/color]


    


),

)); ?>

Help me please, what is the problem

Excuse my English, I’m French

You assume that StatutDemande::model()->findByPk($model->statut) always returns an object which apparently is not true, that is there is no related record.

Also, you should just use relations, not fetch records manually by providing the FK value. Read the guide about relations.

Hi nineinchnick

Thank you for taking the time to respond. I find the reason why I had this error. In fact, some record of the table "demande" in the database did not receive the foreign key from "statut". (I had allowed zero values ​​for testing and I forgot to put the clause not null). thank you for the tutorial, it is very rich. it will help me for the future.