Thursday, 18 December 2014

Accessing the related field in CGRID VIEW of YII -- making related field as searchable

Please refer the link as follows: 


http://www.yiiframework.com/wiki/281/searching-and-sorting-by-related-model-in-cgridview/

The working Example I have done as follows:

In Model (Inward.php) :

First of all define the relations with other models as follows (foreign key not required for HAS_ONE)

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(
            'refname'=>array(self::HAS_ONE,'RefMaster',array('id'=>'ref_code')),
            'reftname'=>array(self::HAS_ONE,'RefType',array('id'=>'ref_tcode')),
            'sub'=>array(self::HAS_ONE,'SubMaster',array('id'=>'sub_code')),
        );
    }


Now , Define a public variable which is used for our search :

.. Rest of code .....

class Inward extends CActiveRecord
{
    public $sub_search;
.....
.......


In rules add the varible (sub_search in 'safe' on 'search' ) example:

public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
           ......
           ........
array('fms_rno, apl_name, inw_no, inw_year, ref_no, ref_code, sub_code, sub_desc, sec_code, sub_scode, reg_date, fil_stat, clo_cat, clo_date, due_date, mobile, iflag, sec_rdt, ref_tcode, sub_imp, clo_rem, rec_flag, pages, rec_date, wee_out, wee_date, logd, stat_code, stat_rem, stat_udate, mer_file, mer_date, id, sec_fno, sec_year, sec_rem, sup_rem, lpo_rem, sdc1_rem, sdc2_rem, ajc_rem, dro_rem, ao_rem, mainsec_code,mainsub_scode,section,nfp,cfp,tot,pend_with,ref_tname,clo_yn,sub_search', 'safe', 'on'=>'search'),
        );
    }

Define attribute Labels for the public variable for the sake of  readability in gridview:

public function attributeLabels()
    {
        return array(
                ..........
                ..........
               'sub_search'=>'Subject',
        );
    }

Finally Our search criteria :

Modify the search function as follows:

public function search()
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria=new CDbCriteria;

                   /*  The below condition is get files which are pending at the logged in user   */

               $userName=Yii::app()->user->name;                                 // getting the user name
               $tk=User::model()->findAll("username='userName'");          
               foreach($tk as $t){
                       $tsec_code=$t->sec_code;                                           // getting the pending status
                       $tsub_scode=$t->sub_scode;
                }

     if($userName<>'admin')                          //  getting the status of the files whether they are pending at logged user or not . The condition wont work for admin user.
            $criteria->condition="sec_code=$tsec_code AND clo_yn='N' AND sub_scode=$tsub_scode";
    else
            $criteria->condition="clo_yn='N'";                // displaying only open files

             /*  Upto here user pendency cheking  */
       
            $criteria->with = array('sub');                   // Here is our relation. Intimating our created relation name to criteria. 'sub' is the relation name defined in relations method

              ...................
              .................
        $criteria->compare('mainsub_scode',$this->sub_scode);
        $criteria->compare('sub.sub_name',$this->sub_search,true);     // Add our public variable to compare criteria

           /* Modify ActiveDataProvider as follows we can add sort function also please check the link */

        return new CActiveDataProvider($this, array(
            'criteria'=>$criteria,                        // add this to dataProvider to send the subject name instead of sub_code
        ));
    }
          

/* IN VIEW (admin.php)  */

Change the CgridView of admin.php as follows:

<?php $this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'inward-grid',
    'dataProvider'=>$model->search(),
    'filter'=>$model,
    'columns'=>array(
        'reg_date',
        'inw_no',
        'inw_year',
         'ref_tname',
         array('name'=>'sub_search','value'=>'$data->sub->sub_name'),             // giving the search value to compare
        'ref_no',
        'apl_name',
        'sub_desc',
        'pend_with',
        'sec_fno',
        'sec_year',
        'section',
        array(
            'class'=>'CButtonColumn',
            'template'=>$format,
        ),
    ),
)); ?>

No comments:

Post a Comment