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: 164
  • 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 online   PeRoChAk 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 525
  • Joined: 26-November 10
  • Location:Sialkot, 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: 164
  • 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 online   PeRoChAk 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 525
  • Joined: 26-November 10
  • Location:Sialkot, 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 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 599
  • 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: 164
  • Joined: 04-October 12

Posted 26 January 2013 - 01:16 PM

OK , Thanks
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