Search code examples
phpactiverecordyiiyii-cmodel

Yii Lazy Loading Getting values in through table/model


In Yii, I have a relations set up that use the through option.

public function relations()
    {
        return array(
            'users_relationships' => array(
                self::HAS_MANY, 'EnvironmentUsers', 'environment_id', 'together'=>true 
            ),
            'users' => array(
                self::HAS_MANY, 'Users', array('user_id' => 'user_id'), 'through' => 'users_relationships', 'together'=>true 
            ),

        );
    }

And I access it like :

 foreach($model -> users as $value):
    $model-> user_id;
    endforeach;  

Which works fine when I want to access values in the Users model. But when I try to access a value in the EnvironmentUsers model, it throws the notice value cannot be found.

foreach($model -> users as $value):
    $model-> is_environemnt_administrator;
    endforeach;

My question is, how can I also access the the values in the through table when lazy loding in Yii?


Solution

  • In your second loop you should call the good related model! It seems you are calling users instead of users_relationships

    foreach($model->users_relationships as $value):
        $model-> is_environemnt_administrator;
        endforeach;