[Solved] Problem With Relation Model

Hello all,

I am French and advance sorry for my english.

I have two table "Promotion" and "specialite".

I want to display the title of my specialty for each promotion.

Here are my relationships :

PROMOTION model:

[HTML]

public function relations() {

    return array(


        'specialite'=>array(self::BELONGS_TO, 'Specialite', 'id_specialite'),


        'filiere'=>array(self::HAS_ONE, 'Filiere', 'id_filiere',


        'through'=>'specialite'),


    );


}

[/HTML]

and SPECIALITE model :

[HTML]

public function relations() {

    return array(


        'filiere'=>array(self::BELONGS_TO, 'Filiere', 'id_filiere'),


        'promotions'=>array(self::HAS_MANY, 'Promotion', 'id'),


    );


}

[/HTML]

And here my code :

[HTML]

$test = Promotion::model()->with(‘specialite’)->findAll();

    foreach($test as $tt) {


        echo ($tt->date_debut.' : ');


        echo ($tt->specialite->intitule.'<br/>');


    }

[/HTML]

But I have an error message :

[HTML]

Trying to get property of non-object

on this ligne : echo ($tt->specialite->intitule.’<br/>’);

[/HTML]

Thank you in advance for your help

Best regards

Arteckos

Hi Arteckos

Are you sure the $tt->specialite is not NULL?

Maybe a Promotion has not at least one specialite, so a NULL specialite thows an exception when calls $tt->specialite->intitule

you could avoid it by this


if ($tt->specialite) echo $tt->specialite->intitule;

Thank you very much for your answer KonApaz.

Indeed, some elements are null value.

And with your test, it works.

Thanks angain.

Do you know how to put this post resolved ?

Best regards

Arteckos

Welcome! I think you should modify your thread title appending the ‘[Solved]’

If anyone knows about it, please inform us :)