Yii Framework Forum: How To Load Relationshep Values In Listdata() - Yii Framework Forum

Jump to content

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

How To Load Relationshep Values In Listdata() Rate Topic: ***** 1 Votes

#1 User is offline   samilo 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 231
  • Joined: 04-October 12

Posted 26 January 2013 - 05:03 AM

Hi

I need to know how to call relationship in listData() ,

For Example in normal way we can call it like :

$model->section->section_name ;


in my code i just print value as number but I need to print string value from courses table .


   public function coursesList($section_id) {
    	
    	
    	$data=Msection::model()->findAll('section_id=:section_id', 
              	array(':section_id'=>(int)$section_id));
          	
              	
    	return CHtml::listData($data,'course_id','course_id');
 //// can i call it like : ///listData($data,'course_id->course->course_name','course_id');
	}


Can I call it like?
listData($data,'course_id->course->course_name','course_id');


How To fix that ?

thanks in advance
0

#2 User is offline   PeRoChAk 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 578
  • Joined: 26-November 10
  • Location:Lahore, Pakistan

Posted 26 January 2013 - 05:38 AM

Yes, you can do it as

listData($data,'course.course_name','course_id');


where course is a relation.

Further Value funcion
Cheers
Perochak
Web Design - Logo Design - Application Development

My Blog | Forums Comparison
1

#3 User is offline   samilo 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 231
  • Joined: 04-October 12

Posted 26 January 2013 - 08:09 AM

Thanks PeRoChAk but Not Work , This is my relations , it's just print course_id when use your code
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(
			'section' => array(self::BELONGS_TO, 'Csection', 'section_id'),
			'course' => array(self::BELONGS_TO, 'Courses', 'course_id'),
		);
	}

0

#4 User is offline   PeRoChAk 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 578
  • Joined: 26-November 10
  • Location:Lahore, Pakistan

Posted 26 January 2013 - 09:03 AM

Can you show your code here
Cheers
Perochak
Web Design - Logo Design - Application Development

My Blog | Forums Comparison
0

#5 User is offline   seenivasan 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 620
  • Joined: 17-June 12
  • Location:Chennai,TamilNadu,India.

Posted 26 January 2013 - 09:31 AM

Dear Friend
I think what PeRoChAk suggested should work.

Else Kindly check the following in your case please.

$sections=Msection::model()->findAll('section_id=:section_id', array(':section_id'=>(int)$section_id));

CHtml::listData($sections,function($section){ //You can pass the anonymous functions from second parameter on wards.
       return CHtml::encode($section->course->course_name);
                                            },'course_id');


Regards.
1

#6 User is offline   samilo 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 231
  • Joined: 04-October 12

Posted 26 January 2013 - 01:16 PM

OK , Thanks
0

#7 User is offline   casamajor 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 23-April 14

Posted 27 April 2014 - 08:12 PM

In my use case, the current model has a HAS_MANY relationship with the model whose attribute I need to access, so I cannot get a single value as an attribute of this relation. But I have this value defined in a variable already, as the listData function is actually within a foreach loop that iterates this relation.

I came up with this workaround that let me access that related attribute:

  • Add a BELONGS_TO relation to the pivot model.
  • Use the pivot model instead of the real model.
  • This way the pivot model is passed over to the anonymous function, so you can access the relation's attribute directly.


It works but I would like to know whether there is a more straightforward solution. Is there any simpler way to access another variable from within the anonymous function?
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