In Listing How To Fetch Data From Multiple Table

Hi,

I have two table student and student group. student_group_id is the foreign key in student table. Now listing of student table i want to show student group name(group_name) instead of student group id. Please tell me where i will change the code.

In student table:


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(

			'm_student_group' => array(self::HAS_ONE, 'm_student_group', 'Group_Id'),

		);

	}

In student group table:


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(

			'm_student' => array(self::HAS_ONE, 'm_student', 'Group_Id'),

		);

	}

Please tell me, how to solve this problem.

ohhh man, problem with your relations, use BELONGS_TO in child table model.

// Post model relations

  1. ‘user’ => array(self::BELONGS_TO, ‘User’, ‘user_id’),

// Post belongs_to a user, because it is a child table.

// Profile model relations

  1. ‘user’ => array(self::BELONGS_TO, ‘User’, ‘user_id’),

// Profile belongs_to a user, because it is a child table.

// User model relations

  1. ‘posts’ => array(self::HAS_MANY, ‘Post’, ‘user_id’),

// User has_many posts, because User is a parent table.

  1. ‘profile’ => array(self::HAS_ONE, ‘Profile’, ‘user_id’),

// User has_one profile, because User is a parent table.

for more information:

http://www.yiiframework.com/wiki/181/relations-belongs_to-versus-has_one/

Hi Iqbal,

write the following code in two pages.

In StudentGroup:


public function relations()

	{

		return array(

			'student' => array(self::HAS_MANY, 'Student', 'Group_ID'),

		);

	}



In Student:


public function relations()

	{

		return array(

			'student_group' => array(self::BELONGS_TO, 'StudentGroup', 'Group_ID'),

		);

	}

But not show as following in list page.




.....:......

Group:GROUP NAME

.....:..........



Please help me.

Hi,please see this forum

I hope it’s some help.