How to get data from an object read by the relation

I run this to populate the object


$this->accountactivation = account_activation::model()->with('residence', 'user','user.details')->find(

                                array(

                                    'condition' => 'activation_code=:code and t.zip_code=:zipcode',

                                    'params' => array(':code' => $this->registration_code, ':zipcode' => $this->zipcode))

                );

Then I want to use it like


echo $this->accountactivation->residence->address1;

But nothing happens, always empty value is returned even if the DB has value.

When I do print_r on the object I can see the value it has. http://screencast.com/t/N5wAmQkdj

What I am doing wrong?

I have same problem. Please, any expert to response?

You need to give more information. The code you showed seems correct. Please show the relation definition in accoun_activation model. Also make sure that there’s really a related record. You could also check the SQL created (configure a CWebLogRoute and enable YII_DEBUG).

As you can see in the posted screenshot (see the first post), there is a related record, and the record is read very fine, as those details are real. But simply when accessing doesn’t return anything.

The relation is defined as:


 public function relations() {

        // NOTE: you may need to adjust the relation name and the related

        // class name for the relations automatically generated below.

        return array(

            'residence' => array(self::MANY_MANY,'residences', 'user_details(user_id,residence_id)',

            'user' => array(self::BELONGS_TO, 'user',

                'user_id'),

        );

    }

MANY_MANY will return an array




echo $this->accountactivation->residence[0]->address1;



/Tommy