M
M
Monitorkin2017-03-28 09:55:02
MySQL
Monitorkin, 2017-03-28 09:55:02

Filters in GridView. leftJoin and andFilterWhere how to output?

I do not understand how to force the filter to be displayed. Please help with setting up a filter for related fields.
The SearchModel builds a query from two tables ' shop_product' (product_id, published, etc.) - products and 'shop_product_value' (product_id, param_id, value) - product parameter values ​​that are linked by product_id :

$query = (new \yii\db\Query())
    		->select(['p.product_id', 'Опубликован' => 'p.published', 'Price' => 'p1.value', 'Description' => 'p2.value'])
    		->from(['p' => 'shop_product'])
    		
    		->leftJoin('shop_product_value AS p1', 'p.product_id = p1.product_id AND p1.param_id IN (2)')
    		->leftJoin('shop_product_value AS p2', 'p.product_id = p2.product_id AND p2.param_id IN (5)')
    		
    		->indexBy('product_id');

Further:
$dataProvider = new ActiveDataProvider([
    			'query' => $query,
    			'pagination' => [
    				'pageSize' => 10,
    			],
    		]);
    		...
    		$query->andFilterWhere([
    			'p.product_id' => $this->product_id, // генерирует поле фильтра
    			'p.published' => $this->published,  //фильтра нет!
    			'p1.value' => ???????, //фильтра нет!
    
    		]);
    		...
    		return $dataProvider;

- How to force the filter to work in such request?
- Why if you add the alias 'Published' => 'p.published', the filter is no longer displayed, but if you remove 'Published' then the filter appears?
- How to display the filter for fields from leftJoin???
GridView for the time being, for simplicity, I display it like this:
GridView::widget([
    		'dataProvider' => $dataProvider,
    		'filterModel' => $searchModel,
    		//'columns' => $columns,
    	]);

I would be grateful for the help, I have already tried everything I can :-(...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Timofeev, 2017-03-28
@webinar

you need to add public attributes to the model, describe them in rules, for sorting, describe sort in dataProvider
read:
www.yiiframework.com/wiki/653/displaying-sorting-a...
www.yiiframework.com/wiki/621/filter- sort-by-calculated

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question