[solved]CGridView an a not filled related record

Hi Yii fans,

I’m building a flight application. In my flight model, I’ve defined two relations:

		return array(

			'pilote'=>array(self::BELONGS_TO, 'Annuaire','cdtbord'),

			'coPilote'=>array(self::BELONGS_TO, 'Annuaire','copilote'),


In my flight model, cdtbord field is required but copilote not.

For a solo flight (no copilot), a new created row set copilote field to 0. That value has no related record in model Annuaire.

In my gridview for displaying the flights:


		   	'header'=>'Co pilote',



			'filter'=>CHtml::activeDropDownList($model, 'copilote', CHtml::listData(Vol::model()->findAll(),'copilote','coPilote.nom_prenom'),array('prompt'=>'','class'=>'span3')),


drop me an error saying "Trying to get property of non-object " while fetching the row with copilote field set to 0 … Not surprising in fact.

I’ve tried something like:


no errors, but filled fields aren’t displayed at all …

Some help will be very appreciate.

Regards from France



Thank you for help, but this does not work.

unfortunately, it looks like the conditional statements aren’t evaluated.



Dollar sign missing.

Conditional statements are evalueted, I used before.

If it isn’t the missing dollar sign. Maybe another problem, not the coditional statement, try:

'value'=>'(true)?"OK":"Not OK"',

'value'=>'(false)?"OK":"Not OK"',

Instead of




This way you check if the relation coPilote has any value… and display the nom_prenom only when it does.


Exact, I haven’t figured it out, and … hurra, it works.

Many thanks andredelorme

@mdomba, thank you for helping me.

default AR of flight model is to populate the copilote table with with a 0 value, so (well that’s how I understand it)


is not working ("Trying to get property of non-object ")

Topic marked as [solved]

Why would you not make the copilote column nullable in your database and use null to represent a non-existant copilot?

That’s basically what null is for…


that’s true, and finally, this is what I have done, using mdomba’s solution.

Regards from France.