value from a function in GridView + searchable

Hi all,

In DetailView I have no problems displaying the value by getting it from a function like this:




[

	'attribute' => 'privats',

	'label'=> 'Privāts/Juridisks',

	'value'=> $model->getPrivJurStr(),


],



The function in model is:




public function getPrivJurStr()

{

	if($this->privats == 1){

		return "Privāts";

	}else{

		return "Juridisks";

	}

}



When trying to do the same in index.php GridView I get an

I have added the same function in the searchmodel and using that.

Under GridView widget in index.php I have it like this:




<?= GridView::widget([

        'dataProvider' => $dataProvider,

        'filterModel' => $searchModel,

        'columns' => [

            ['class' => 'yii\grid\SerialColumn'],

              //some other attributes

            [

            	'attribute' => 'privats',

            	'label' => 'Privāts/Juridisks?',

            	'value' => $searchModel->getPrivJurStr(),

            ],

            // rest of the code



In table they are stored as 1 and 0 and I want to display them as "Privats" "Juridisks" and also search by those strings instead of 1 and 0. Is this possible using the current structure I have? Or do I need to make another table containing id>value (1>privats,0>juridisks) and link the tables?

Thank you! :)

Hi, for display your data in GridView instead of value -




'content' => function($model) {

   return $model->getPrivJurStr();

}



Thank you very much, works like a charm! :)

Any clue how I could get the search working by those strings?

Currently it’s expecting an integer value.

I’ll probably have to amend the SearchModel, right?

If you don’t won’t to use relation, you can use constants.

Try this approach.

In your model:




    const STATUS_JURIDICAL = 0;

    const STATUS_PRIVATE = 1;


    public static function getStatusOptions()

    {

        return [

            self::STATUS_JURIDICAL => 'Privāts',

            self::STATUS_PRIVATE => 'Juridisks',

        ];

    }


    public function getAllowedStatusRange()

    {

        return [

            self::STATUS_JURIDICAL,

            self::STATUS_PRIVATE

        ];

    }


    public function getPrivJurStr()

    {

        return ArrayHelper::getValue(self::getStatusOptions(), $this->privats, 'expected value not exist'));

    }




In GridView:




    [

        'attribute' => 'privats',

        'filter' => YourModelClassName::getStatusOptions(),

        'label' => 'Privāts/Juridisks?',

        'content' => function($model) {

           return $model->getPrivJurStr();

        }




    ],




In SearchModel:




    public function rules()

    {

        return [

            [['privats'], 'integer'],

        ];

    }


    $query->andFilterWhere([

            'privats' => $this->privats,

        ]);




And when save the model you can add rules to attr. privats.




    ['privats', 'in', 'range' => self::getAllowedStatusRange()],



Thank you very much, Rom, that works exactly as I wanted!!!

You’re the best! :)