Passing Url Value With A Twist

I have 3 models: Person , Event and Person_event (a connection table which has person_id and event_id)

In the view of every Event I show a CgridView of the people who were added to that same Event.

I’m able to show the grid of the people , but obviously I want to show only the people for that same event.

I’v changed the search function in the model so that it will get a ‘condition’ and I’v managed to set it by hand (for example: show me only the people of event_id 34)

What I can’t do is to pass the event’s id from the url (which is being passed for the view page) to the condition. I’v tried 4-5 methods which I found on the forum but none worked. Can it be because I’m using a Person’s grid in vent’s view?

here is the grid with the condition set by hand:


<?php $this->widget('zii.widgets.grid.CGridView', array(

	'id'=>'person--event-grid',

	'dataProvider'=>Person_Event::model()->search(array('condition'=>'34')),

	//'filter'=>$model,

	'columns'=>array(

		. . . . . 

Any ideas?

I tend to do something like this in the action:




    public function actionList($eventId)

    {

        $personEventFilter = new Person_Event('search');


        if (isset($_GET['Person_Event']))

            $personEventFilter->attributes = $_GET['Person_event'];


        $personEventFilter->event_id = $eventId;


        // ...

    }



This way, you can just treat the event ID as a standard searchable field; you don’t need any special processing or to pass additional parameters into the search() method.

Hello Keith.

Please excuse me for being a newbie, but I’m quite confused by the code.

Should I write it in a new action or add to an existing one?

From where and how the Person_Event is being sent?

How to I connect this action to the CgridView in Event’s view?

I tried adding this code to the actionView (which gets the $id anyway) , but Cgridview still shows all the people disregarding the event_id.

Thanks ,

Mark.

You should post your search() method and your model’s rules.

I see.

Meanwhile, in the CgridView , I managed to echo the id in the URL with this line:


$id = Yii::app()->request->getQuery('id');

However when I put $id in the condition


'dataProvider'=>Person_Event::model()->search(array('condition'=>'event_id=$id')),

I get an SQL error “Column not found: 1054 Unknown column ‘$id’” .

It looks like the problem is in the ‘condition’ which can’t compare fields with a variable. Can it?

I mean you should post the contents of your rules and your search method here so people can help to debug them. :)

Oh , sure.

Person_Event model search:


public function search($param = array())

	{

		$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('participated',$this->participated);

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

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

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


		return new CActiveDataProvider($this, array(

			'criteria'=>$criteria,

		));

	}

No changes were made to the rules.

You should be able to pass in variables like this


'dataProvider'=> Person_Event::model()->search(array('condition'=>'event_id=:id', 'params' => array(':id' => $id))),

At least for the SQL error, replace your single quotes around ‘event_id=$id’ to “event_id=$id”, it would take care of that part of the error.

Wow , that solved the problem. Now the condition is working well!

I’m quite baffled - why the single quote made such a difference?

Thanks for everyone,

Mark.

Glad it worked for you.

Double quotes allow interpolation, which means for any variable defined in a double quote its value would be used.

For instance “event_id=$id” is same as ‘event_id=34’, however your earlier string with single quote did not allow interpolation hence it was taken as ‘event_id=$id’.