Yii Framework Forum: How to write in criteria? - Yii Framework Forum

Jump to content

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

How to write in criteria? Rate Topic: -----

#1 User is offline   holala 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 74
  • Joined: 23-December 09

Posted 02 March 2010 - 04:49 AM

there have 3 tables,group,employee and groupemployee,Group and Employee are models.the relation is:
in model group
	public function relations()
	{
		return array(
			'employees' => array(self::MANY_MANY, 'Employee', 'groupemployee(group_id, employee_id)'),
		);
	}


in model employee
	public function relations()
	{
		return array(
			'groups' => array(self::MANY_MANY, 'Group', 'groupemployee(group_id, employee_id)'),
	}


in controller Group,I want get all employees in the group,the group id is 1.

$dataProvider=new CActiveDataProvider('Group', array(
	'criteria'=>array(
		...
	),
	'pagination'=>array(
		'pageSize'=>self::PAGE_SIZE,
	),
));

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

I don't know How to write in the criteria,please help,Thanks!
0

#2 User is offline   got 2 doodle 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 248
  • Joined: 12-December 08
  • Location:Beautiful Nova Scotia, Canada

Posted 02 March 2010 - 02:34 PM

    $dataProvider=new CActiveDataProvider('EventParticipant', array(
                 'criteria'=>array('condition'=>'status_id=:status AND participant_level_id=:participantLevel AND event.contact_id=:PRG',
                 'params' =>array( ':status'=>1,
                 ':participantLevel'=>1,
                 ':PRG'=>$PrgID),
                 'order'=>'event_id',
                 'with'=>array('event','participant')
                                                      ),
                  'pagination'=>array('pageSize'=>self::PAGE_SIZE),
                                                  ));


This is just a cut/paste of my code hopefully it helps you out. In my case all of the items are in the many/many table 'EventParticipant'

EventParticipant relations

	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(
			'participant' => array(self::BELONGS_TO,
			                             'Participant',
			                             'participant_id',
			                             ),
			                                                           
			'event' => array(self::BELONGS_TO,
			                              'Event',
			                              'event_id',
			                              'joinType'=>'INNER JOIN',
			                               ),
			'status' => array(self::BELONGS_TO, 'Status', 'status_id'),
			'participant_level' => array(self::BELONGS_TO, 'ParticipantLevel', 'participant_level_id'),
			'payment_method' => array(self::BELONGS_TO, 'PaymentMethods', 'paymentMethods_id'),
		);
	}


Event model relations
	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(
			'contact' => array(self::BELONGS_TO, 'Participant', 'contact_id'),
			'event_type' => array(self::BELONGS_TO, 'EventType', 'event_type_id'),
			'event_location' => array(self::BELONGS_TO, 'EventLocations', 'event_location_id'),
			'event_status' => array(self::BELONGS_TO, 'EventStatus', 'event_status_id'),
			'eventParticipants' => array(self::HAS_MANY, 'EventParticipant', 'event_id'),
			'eventThemes' => array(self::HAS_MANY, 'EventTheme', 'event_id'),
		);
	}


participant relations
	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(
			'donations' => array(self::HAS_MANY, 'Donation', 'participant_id'),
			'events' => array(self::HAS_MANY, 'Event', 'contact_id'),
			'eventParticipants' => array(self::HAS_MANY, 'EventParticipant', 'participant_id'),
			'gender' => array(self::BELONGS_TO, 'Gender', 'gender_id'),
			'lodgingtype' => array(self::BELONGS_TO, 'LodgingType', 'lodgingtype_id'),
			'participantCreditCards' => array(self::HAS_MANY, 'ParticipantCreditCard', 'participant_id'),
			'participantFoodHealths' => array(self::HAS_MANY, 'ParticipantFoodHealth', 'participant_id'),
			'users' => array(self::HAS_MANY, 'Users', 'participant_id'),
			 
			'participantLevel'=>array(self::MANY_MANY, 'ParticipantLevel', 'EventParticipant(participant_id, participant_level_id)'),
			

		);
	}

doodle
Check out myYii powered website
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