How To Get Value By Relation Name

Hello all:

I have a model have some relations ,Now I want to get every relation’s value ,such as this:




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(

		'worktime_user'=>array(self::BELONGS_TO,'User','customer'),

		'worktime_user1'=>array(self::BELONGS_TO,'User','principal'),

	);

}


public function getRelationValue($model)

{

    $rs = $model->getMetaData()->relations;

    foreach ( $rs as $rel)

    {

         //Get related model User's name ,but the result is  'worktime_user.name', not the real value; 

         echo $model->getAttribute($r->name.'.name');

    }

}



this code has no effect, how can i do is right?

[color="#FF0000"]

//Get related model User’s name ,but the result is ‘worktime_user.name’, not the real value;

     echo $model->getAttribute($r->name.'.name');[/color]

Many thanks!!

Maybe this would work?




    foreach (array_keys($model->relations()) as $relationName)

    {

        echo $model->$relation->name;

    }



This is a strange thing to do though. What’s the reasoning behind it?

EDIT

Alternatively:




    foreach (array_keys($model->relations()) as $relationName)

    {

        echo $model->getRelated($relationName)->name;

    }



Solved by myself :

echo $model->getRelated($r->name)->name

To Keith :

I want to compare two model ,which attribute’s value is changed ,then save the change log to db.

Such as :

Tom change the status : from Open to Closed at 2014/03/29 19:30:00

Jerry change the customer : from IBM to Microsoft at 2014/03/20 18:30:00