[Krajee Extension] DataGrid with related model data

Hi All,

I have the following model with relations:


class Testone extends ActiveRecord {

	

	public $testtwo;

	

	public function getTestwo() {

		return $this->hasOne(Testtwo::className(), ['id' => 'id_test']);

	}


	public function rules() {

		return [[['atr1', 'atr2', 'testtwo'], 'safe']];

	}


	public function search($params) {

		$query = Testone::find();

		$query->joinWith(['testtwo']);


		$dataProvider = new ActiveDataProvider([

			'query' => $query,

			'pagination' => [

				'pageSize' => 10,

			],

		]);

		

		// walidacja parametrów wyszukiwania

		if (!($this->load($params) && $this->validate())) {

			return $dataProvider;

		}

		

		$query->andFilterWhere(['like', '{{%testtwo}}.value', $this->testtwo]);

		

		return $dataProvider;

	}



I display related column in the component Krajee DataGrid:


$relatedColumns = [[

	'attribute' => 'testtwo',

	'value' => 'testtwo.value',

	'class' => DataColumn::className(),

]];

Here there is a problem. Filter for the ‘testtwo’ column works properly. But I do not know why the cell’s value is always “(not set)”. When I change the ‘testtwo’ variable type (from public to protected) the data set properly but filter’s value is lost after ajax reloading.


public $testtwo;

// filter's field works properly but data is not set




protected $testtwo;

// filter's field lose the value after reloading but data is set



What am I doing wrong? Please, help.

Thank you in advance.

PS. Sory for my english :confused:

change




public $testtwo;



to




private $_testtwo;


public function setTesttwo($val) {

    $this->_testtwo = $val;

}



and




$query->andFilterWhere(['like', '{{%testtwo}}.value', $this->testtwo]);



to




$query->andFilterWhere(['like', '{{%testtwo}}.value', $this->_testtwo]);



or maybe better change




public $testtwo;



to




public $testtwoSearch;



and




$query->andFilterWhere(['like', '{{%testtwo}}.value', $this->testtwo]);



to




$query->andFilterWhere(['like', '{{%testtwo}}.value', $this->testtwoSearch]);



and




$relatedColumns = [[

        'attribute' => 'testtwo',

        'value' => 'testtwo.value',

        'class' => DataColumn::className(),

]];



to




$relatedColumns = [[

        'attribute' => 'testtwoSearch',

        'value' => 'testtwo.value',

        'class' => DataColumn::className(),

]];



[member=‘Dawid Partyka’], Thanks for your reply. Unfortunately your solution doesn’t work.

Have you any other sugestions?