Gridview - Filter Column From 1:n Relation

Hello,

I have a 1:N relation that I leave below.

On the admin.php of the tbl_pessoa I can add the column that have all the values that I need from tbl_acordo. In this case for every record in tbl_pessoa I need to see all values that the "referencia" field has in tbl_acordo.

And in this case, this Person has 2 related records in tbl_acordo

The only problem is, that I can’t filter this column that I added. Can anyone shed me some light in this issue?

Thanks.

PS: those black squares on the images are made because I’m working with real values.

admin.php


<?php

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

    'id' => 'pessoa-grid',

    'dataProvider' => $model->searchByEmpresa($_GET['idempresa']),

    'filter' => $model,

    'columns' => array(

        'nome',

        'nif',

        array(

            'name'=>'referencia',

            'type'=>'raw',

        ),

        array(

            'class' => 'CButtonColumn',

            'template' => '{acordos}&nbsp;&nbsp;{view}&nbsp;&nbsp;{update}&nbsp;&nbsp;{delete}',

            'htmlOptions' => array('style' => 'width:85px'),

            'buttons' => array(

                'delete' => array(

                    'visible' => 'PessoaController::checkAcessWModel($data->id) > 3'

                ),

                'update' => array(

                    'visible' => 'PessoaController::checkAcessWModel($data->id) > 2'

                ),

                'view' => array(

                    'visible' => 'PessoaController::checkAcessWModel($data->id) > 1'

                ),

                'acordos' => array(

                    'visible' => 'PessoaController::checkAcessWModel($data->id) > 2',

                    'label' => 'Ver Acordos',

                    'imageUrl' => Yii::app()->request->baseUrl . '/images/rar_16.png',

                    'url' => 'Yii::app()->createUrl("acordo/admin", array ( "idpessoa" => $data->id))',

                ),

            ),

        ),

    ),

));

?>

(Model)

Pessoa.php


class Pessoa extends CActiveRecord {

    public $referencia;


    /**

     * Returns the static model of the specified AR class.

     * @return Pessoa the static model class

     */

    public static function model($className=__CLASS__) {

        return parent::model($className);

    }

    

    /**

     * @return string the associated database table name

     */

    public function tableName() {

        return '{{pessoa}}';

    }


    /**

     * @return array validation rules for model attributes.

     */

    public function rules() {

        // NOTE: you should only define rules for those attributes that

        // will receive user inputs.

        return array(

            array('nome, morada, concelho, codpostal, nif, bi, idempresa', 'required'),

            array('nif, bi, idempresa', 'numerical', 'integerOnly' => true),

            array('nome, email', 'length', 'max' => 250),

            array('concelho', 'length', 'max' => 150),

            array('codpostal', 'length', 'max' => 30),

            array('telefone, telemovel, fax', 'length', 'max' => 50),

            array('info', 'safe'),

            // The following rule is used by search().

            // Please remove those attributes that should not be searched.

            array('id, nome, morada, concelho, codpostal, nif, bi, telefone, telemovel, email, fax, info, idempresa', 'safe', 'on' => 'search'),

        );

    }


    /**

     * @return array relational rules.

     */

    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(

            'acordos' => array(self::HAS_MANY, 'Acordo', 'idpessoa'),

            'idempresa0' => array(self::BELONGS_TO, 'Empresa', 'idempresa'),

        );

    }


    /**

     * @return array customized attribute labels (name=>label)

     */

    public function attributeLabels() {

        return array(

            'id' => 'ID',

            'nome' => 'Nome',

            'morada' => 'Morada',

            'concelho' => 'Concelho',

            'codpostal' => 'Codpostal',

            'nif' => 'Nif',

            'bi' => 'Bi',

            'telefone' => 'Telefone',

            'telemovel' => 'Telemovel',

            'email' => 'Email',

            'fax' => 'Fax',

            'info' => 'Info',

            'idempresa' => 'Idempresa',

        );

    }


    /**

     * Retrieves a list of models filtered by idempresa based on the current search/filter conditions.

     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.

     */

    public function searchByEmpresa($idempresa) {

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

        // should not be searched.


        $criteria = new CDbCriteria;


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

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

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

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

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

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

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

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

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

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

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

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

        $criteria->compare('idempresa', $idempresa);

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


        return new CActiveDataProvider(get_class($this), array(

            'criteria' => $criteria,

        ));

    }

    

    public function getAcordos($acordos){

        $arc='';

        foreach ($acordos as $a){

            $arc .= $a->referencia .' ';

        }

        return $arc;

    }

    public function beforeFind() {

        $this->referencia = $this->getAcordos($this->acordos);

        parent::beforeFind();

    }


    public function afterFind() {   

        parent::afterFind();

        $this->referencia = $this->getAcordos($this->acordos);

    }

    

}

I was missing something on my compare. Thanks to my buddy n3okill that solved this issue.


    /**

     * @return array validation rules for model attributes.

     */

    public function rules() {

        // NOTE: you should only define rules for those attributes that

        // will receive user inputs.

        return array(

            array('nome, morada, concelho, codpostal, nif, bi, idempresa', 'required'),

            array('nif, bi, idempresa', 'numerical', 'integerOnly' => true),

            array('nome, email', 'length', 'max' => 250),

            array('concelho', 'length', 'max' => 150),

            array('codpostal', 'length', 'max' => 30),

            array('telefone, telemovel, fax', 'length', 'max' => 50),

            array('info', 'safe'),

            // The following rule is used by search().

            // Please remove those attributes that should not be searched.

            // i needed to add the var that i added on the model.

            array('id, nome, morada, concelho, codpostal, nif, bi, telefone, telemovel, email, fax, info, idempresa, referencia', 'safe', 'on' => 'search'), 

        );

    }


[...]


    /**

     * Retrieves a list of models filtered by idempresa based on the current search/filter conditions.

     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.

     */

    public function searchByEmpresa($idempresa) {

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

        // should not be searched.


        $criteria = new CDbCriteria;


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

        $criteria->compare('t.nome', $this->nome, true);

        $criteria->compare('t.morada', $this->morada, true);

        $criteria->compare('t.concelho', $this->concelho, true);

        $criteria->compare('t.codpostal', $this->codpostal, true);

        $criteria->compare('t.nif', $this->nif, true);

        $criteria->compare('t.bi', $this->bi);

        $criteria->compare('t.telefone', $this->telefone, true);

        $criteria->compare('t.telemovel', $this->telemovel, true);

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

        $criteria->compare('t.fax', $this->fax, true);

        $criteria->compare('t.info', $this->info, true);

        $criteria->compare('t.idempresa', $idempresa);

        

        // the relation acordos needs to be added to the search function to filter the referencia column/field

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

        $criteria->together=true;

        

        $criteria->compare('acordos.referencia', $this->referencia, true);


        return new CActiveDataProvider(get_class($this), array(

            'criteria' => $criteria,

        ));

    }