Yii advanced search not working with specific search function based on the relation

319 views Asked by At

In my system, I have 3 types of user, Admin, Supervisor and Normal user. Every type of user has a Supervisor who is another user itself.
Suppose we have two tables, users and documents and the relation is as follow:enter image description here

Here the documents table is related with users table by user_id column.
In my system, I have a CGridview of Documents module based on the logged in user type. If the logged in user type is Admin, then it shows all the entry of the document table.

If the logged in user type is Supervisor, then it will show only those entry from documents table, where the documents.user_id is equal to those users.user_id, whose users.sp_id = LoggedInUser, sorry if I can't express it clearly.

I can show the data in CGridview by using relation in the Documents model.

public function relations(){
 'users' => array(self::BELONGS_TO, 'Users', 'user_id'),
}

and in the Document model I declare two custom functions to return the desired DataProvider. Those are:

public function searchByUser($id) { $criteria=new CDbCriteria; $criteria->condition = "user_id=$id"; return new CActiveDataProvider($this, array( 'criteria'=>$criteria, 'sort'=>array( 'defaultOrder'=>'doc_id DESC', ), )); }

and

public function searchBySupervisor($supervisor_id)
    {
        $criteria=new CDbCriteria;

        $criteria->with=array('users'); 
        $criteria->compare('user_id',$this->user_id,true);      
        $criteria->condition = "sp_id=$supervisor_id AND status='active'";
        return new CActiveDataProvider($this, array(
            'criteria'=>$criteria,
            'sort'=>array(
                'defaultOrder'=>'doc_idDESC',
            ),
        ));
    }

In my /documents/adminview, I use the dataProvider based on the loggedin user in the CGridview:

if ($UserType=="Normal_User"){
        $this->widget(
        'booster.widgets.TbGridView', array(
        'type' => 'striped bordered condensed',
        'dataProvider'=>$model->searchByUser(Yii::app()->user->getId()),


elseif($UserType == "supervisor"){

        $this->widget(
            'booster.widgets.TbGridView', array(
            'type' => 'striped bordered condensed',
            'dataProvider'=>$model->searchBySupervisor(Yii::app()->user->getId()),

Everything is working fine except my search function isn't working. If I logged in as the supervisor, means if I use this: 'dataProvider'=>$model->searchBySupervisor(Yii::app()->user->getId()), dataprovider, my advance search option doesn't filter the gridview based on the searching parameter, its returning same data.

0

There are 0 answers