Sorting And Filtering With 2 Relations

The question is about when adding columns to CgridView from a relation model.

I’v done that couple of times and all worked.

But now , I’m trying to add columns from 2 different models.

The columns are shown with all the right data , but when I add the relation filters and sorting for both , I’m getting sql errors.

When I switch off one of them , all is working fine for the "active model relation"

Any suggestions how can I have both?


public function search($param = array())

	{

		// Warning: Please modify the following code to remove attributes that

		// should not be searched.


		$criteria=new CDbCriteria($param);


		$criteria->compare('id',$this->id);

		$criteria->compare('person_id',$this->person_id);

		$criteria->compare('event_id',$this->event_id);

		$criteria->compare('confirmed',$this->confirmed);

		$criteria->compare('participated',$this->participated);

		$criteria->compare('link',$this->link,true);

		$criteria->compare('uploaded',$this->uploaded);

		$criteria->compare('review_rating',$this->review_rating);

		

		$criteria->with = array('person');                                                 //getting columns from person

		$criteria->compare( 'person.first_name', $this->fname_search, true );

		$criteria->compare( 'person.last_name', $this->lname_search, true );

		$criteria->compare( 'person.phone', $this->phone_search, true );

		$criteria->compare( 'person.email', $this->email_search, true );

		

		$criteria->with = array('event');                                             //getting columns from event

		$criteria->compare( 'event.name', $this->name_search, true );

		$criteria->compare( 'event.hebDay_id', $this->day_search, true );

		$criteria->compare( 'event.start_time', $this->time_search, true );


		return new CActiveDataProvider($this, array(

			'criteria'=>$criteria,

			'sort'=>array(

        'attributes'=>array(

            'fname_search'=>array(

                'asc'=>'person.first_name',

                'desc'=>'person.first_name DESC',

            ),

			'lname_search'=>array(

                'asc'=>'person.last_name',

                'desc'=>'person.last_name DESC',

            ),

			'phone_search'=>array(

                'asc'=>'person.phone',

                'desc'=>'person.phone DESC',

            ),

			'email_search'=>array(

                'asc'=>'person.email',

                'desc'=>'person.email DESC',

            ),

			

			'name_search'=>array(

                'asc'=>'event.name',

                'desc'=>'event.name DESC',

            ),

			'day_search'=>array(

                'asc'=>'event.hebDay_id',

                'desc'=>'event.hebDay_id DESC',

            ),

			'time_search'=>array(

                'asc'=>'event.start_time',

                'desc'=>'event.start_time DESC',

            ),

            '*',

        ),

    ),

		));

	}

Hi,

I think here you overwrite the previous "with" parameter:


$criteria->with = array('event');

Solution 1:




// ...

$criteria->with = array('person', 'event');

$criteria->compare( 'person.first_name', $this->fname_search, true );

$criteria->compare( 'person.last_name', $this->lname_search, true );

$criteria->compare( 'person.phone', $this->phone_search, true );

$criteria->compare( 'person.email', $this->email_search, true );


$criteria->compare( 'event.name', $this->name_search, true );

$criteria->compare( 'event.hebDay_id', $this->day_search, true );

$criteria->compare( 'event.start_time', $this->time_search, true );

// ...



or

Solution 2:




// ...

$criteria->with = array();

$criteria->with[] = 'person';                                                 //getting columns from person

$criteria->compare( 'person.first_name', $this->fname_search, true );

$criteria->compare( 'person.last_name', $this->lname_search, true );

$criteria->compare( 'person.phone', $this->phone_search, true );

$criteria->compare( 'person.email', $this->email_search, true );


$criteria->with[] = 'event';                                             //getting columns from event

$criteria->compare( 'event.name', $this->name_search, true );

$criteria->compare( 'event.hebDay_id', $this->day_search, true );

$criteria->compare( 'event.start_time', $this->time_search, true );

// ...




$criteria->with = array('person', 'event');

worked fine! .

Before I tried (‘person, event’). . . .