Eric Vieira
Eric Vieira

Reputation: 65

Yii - CgridView Filter stopped working after changing the dataprovider

So, i'm little bit new to Yii. I have a CGridView which looks like:

<?php $this->widget('zii.widgets.grid.CGridView', array(
            'id'=>'proposta-grid',
            'dataProvider'=>Yii::app()->session['user']->listPropostas(),
            'filter'=>$model,
                    ...
                    ...

I changed the dataProvider from $model->search() to User::model()->listPropostas(). The dataProvider and the filter are provided by different models.

This is the code for the listPropostas method, which works fine:

public function listPropostas()
{
    $criteria = new CDbCriteria;
    $arrFiliais = array();
    $arrPropostas = array();

    $arrGrupos = $this->gruposId();
    foreach ($arrGrupos as $grupo) {
        $objGrupo = GrupoDeAnalistas::model()->findByPk($grupo['id']);
        foreach ($objGrupo->listFiliais() as $arr) {
            if (!in_array($arr['id'], $arrFiliais)) {
                array_push($arrFiliais, $arr['id']);
            }
        }
    }

    foreach ($arrFiliais as $arrFilial) {
        $objFilial = Filial::model()->findByPk($arrFilial['id']);
        foreach ($objFilial->listPropostas($this) as $propId) {
            if (!in_array($propId['id'], $arrPropostas)) {
                array_push($arrPropostas, $propId['id']);
            }
        }
    }

    $proposta = new Proposta;
    $criteria->addInCondition("id", $arrPropostas);
    return new CActiveDataProvider($proposta, array(
        'criteria' => $criteria,
    ));
}

So, after I changed the dataProvider the CgridView Filter stopped working

What i'm missing? I know that in the Proposta::model()->search() function, there are sets of criteria comparisons, like $criteria->compare('id',$this->id).

What's the best way to fix this? Change my filter? Add comparisons on listPropostas() function?

Appreciated in advance!

Cheers

Upvotes: 0

Views: 173

Answers (1)

DarkMukke
DarkMukke

Reputation: 2489

Im not sure, you'd have to test it out but I think you are looking for $model->listPropostas() instead User::model()->listPropostas() where listPropostas() should be defined in your Proposta model instead of your User model.

Something like this :

public function listPropostas()
{
    $criteria = new CDbCriteria;
    $arrFiliais = array();
    $arrPropostas = array();

    $arrGrupos = Yii::app()->session['user']->gruposId();
    foreach ($arrGrupos as $grupo) {
        $objGrupo = GrupoDeAnalistas::model()->findByPk($grupo['id']);
        foreach ($objGrupo->listFiliais() as $arr) {
            if (!in_array($arr['id'], $arrFiliais)) {
                array_push($arrFiliais, $arr['id']);
            }
        }
    }

    foreach ($arrFiliais as $arrFilial) {
        $objFilial = Filial::model()->findByPk($arrFilial['id']);
        foreach ($objFilial->listPropostas($this) as $propId) {
            if (!in_array($propId['id'], $arrPropostas)) {
                array_push($arrPropostas, $propId['id']);
            }
        }
    }

    $criteria->addInCondition("id", $arrPropostas);
    return new CActiveDataProvider($this, array(
        'criteria' => $criteria,
    ));
}

Note that the $this keyword in CActiveDataProvider is fairly important, because $this would have data in it while an newly initialised model would have not

Upvotes: 1

Related Questions