Yii Framework Forum: Changing foreach loop into CActiveDataProvider - Yii Framework Forum

Jump to content

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

Changing foreach loop into CActiveDataProvider Rate Topic: -----

#1 User is offline   ajsharma 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 14
  • Joined: 24-October 10

Posted 24 October 2010 - 08:17 PM

Hi folks,

I have the following tables:
  • event
  • event_user
  • group
  • group_user
  • user


It's a little untraditional in that Groups are invited to Events rather than inviting specific users. Thus, each User MANY_MANY Groups and each Group HAS MANY Events. For example, a user could belong to 3 groups, each group has 4 of their own events, so the user should be able to see those 12 events.

Currently, to see the events available to the current user, I use a for loop.
$mygroups = Yii::app()->user->model->groups; //Get's the current user's model, and then their groups
$mygroupsevents = array();
		
foreach($mygroups as $group) {
	$mygroupsevents = array_merge($mygroupsevents, $group->events);
}

$this->render('events', array(
	'events'=>$mygroupsevents,
));


However, I would rather load this data via a CActiveDataProvider so that I can feed it into the various widgets. Does anyone have a more graceful solution for me?

Thanks!
0

#2 User is offline   zaccaria 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 2,232
  • Joined: 04-October 09
  • Location:Moscow

Posted 25 October 2010 - 01:28 AM

You can try to add a inCondition:

$model=new Event('search');
$model->unsetAttributes();  // clear any default values
if(isset($_GET['Event']))
	$model->attributes=$_GET['Event'];
$dataProvider= $model->search();
$dataProvider->criteria->addCondition("id IN (SELECT evenId FROM event_for_groups WHERE group_id IN (SELECT group_id FROM user_in_groups WHERE userId='$userId'))");

$this->render('events', array(
        'model'=>$model,
        'dataProvider'=>$dataProvider,
));

0

#3 User is offline   ajsharma 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 14
  • Joined: 24-October 10

Posted 31 October 2010 - 02:09 PM

Thank you so much! That worked perfectly, I had to alter the search call for my table names like so (for future readers with a similar problem):
$dataProvider->criteria->addCondition("id IN (SELECT id FROM event WHERE groupId IN (SELECT groupId FROM group_user WHERE userId='$userId'))");


I will have to learn more about this cool search 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