Yii Framework Forum: How To Get Value By Relation Name - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

How To Get Value By Relation Name Rate Topic: -----

#1 User is offline   dahuzizyd 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 25
  • Joined: 21-June 11

Posted 28 March 2014 - 10:54 AM

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?

//Get related model User's name ,but the result is 'worktime_user.name', not the real value;
echo $model->getAttribute($r->name.'.name');


Many thanks!!
0

#2 User is offline   Keith 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,670
  • Joined: 04-March 10
  • Location:UK

Posted 28 March 2014 - 11:00 AM

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;
    }

0

#3 User is offline   dahuzizyd 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 25
  • Joined: 21-June 11

Posted 28 March 2014 - 11:04 AM

Solved by myself :

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

#4 User is offline   dahuzizyd 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 25
  • Joined: 21-June 11

Posted 28 March 2014 - 11:17 AM

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
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users