Yii Framework Forum: How to get informations from a join table - 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 informations from a join table Rate Topic: -----

#1 User is offline   luc 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 211
  • Joined: 22-June 10
  • Location:france

Posted 22 June 2010 - 08:23 AM

Hi, my first post here !
I've got an User model, a Group model and a GroupUser model with group_id and user_id (join table). Relations are declared as MANY_MANY in both User and Group models.
Details here:
User class
public function relations()
	{
		return array(
			'group_user'=>array(self::MANY_MANY, 'Group','group_user(group_id, user_id)'),
		);
	}

Group class
	public function relations()
	{
		return array(
			'group_user'=>array(self::MANY_MANY, 'User','group_user(group_id, user_id)'),
		);
	}


Saying:
I want to display in a group action view all the users belongings to that group.

How to achieve that ???

Many thanks
Hey Ho !
Let's go !
0

#2 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,013
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 22 June 2010 - 09:18 AM

$group=Group::model()->with('group_user')->findByPk($yourGroupID);

// to output in view, do something like
foreach($group->group_user as $user)
  echo $user->name;

0

#3 User is offline   mbi 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 604
  • Joined: 08-May 09

Posted 22 June 2010 - 09:59 AM

Group class
public function relations()
{
    return array(
        'users' => array(self::MANY_MANY, 'User','group_user(group_id, user_id)'),
    );
}



$group = Group::model()->findByPk($groupId);
foreach ($group->users as $user) {
    var_dump($user);
}

0

#4 User is offline   luc 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 211
  • Joined: 22-June 10
  • Location:france

Posted 22 June 2010 - 01:48 PM

Hi mbi & Mike,
many thanks,

I'll try your solutions, and keep you in touch.

Luc
Hey Ho !
Let's go !
0

#5 User is offline   luc 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 211
  • Joined: 22-June 10
  • Location:france

Posted 23 June 2010 - 08:38 AM

it works !

AR association are easy in fact :)
Thanks again
Hey Ho !
Let's go !
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