following chapter 6 page 144, i alter search() in Issue model.
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('id',$this->id);
$criteria->compare('name',$this->name,true);
$criteria->compare('description',$this->description,true);
$criteria->compare('type_id',$this->type_id);
$criteria->compare('status_id',$this->status_id);
$criteria->compare('owner_id',$this->owner_id);
$criteria->compare('requester_id',$this->requester_id);
$criteria->compare('create_time',$this->create_time,true);
$criteria->compare('create_user_id',$this->create_user_id);
$criteria->compare('update_time',$this->update_time,true);
$criteria->compare('update_user_id',$this->update_user_id);
// I ADD THIS CODE FOLLOWING THE BOOK
$criteria->condition='project_id=:projectID';
$criteria->params=array(':projectID'=>$this->project_id);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
but this modification cause the filter toolbar in cgridview in admin page not work. can you give me the solution?
i found the solution. the solution is don’t alter search() method in issue model. our modification in actionAdmin() method in issueController class just enough to achieve the goal (to restrict admin search for only current project context)
the book has explain this step-by-step. the wrong one is only instruction for altering search() method in issue model. but as you wanted i’d like to explain it.
this actionAdmin() method part:
/**
* @var private property containing the associated Project model instance
*/
// This addition private variable for retain associated project_id of the issue
private $_project=null;
/**
* Manages all models.
*/
public function actionAdmin()
{
$model=new Issue('search');
$model->unsetAttributes(); // clear any default values
if(isset($_GET['Issue']))
$model->attributes=$_GET['Issue'];
// Assign value for the project_id attribute of $model object by filling it with associated project_id of the issue
// This would restrict admin search for only current project context
$model->project_id = $this->_project->id;
$this->render('admin',array(
'model'=>$model,
));
}