26
Авг
2017

Не работает фильтр в model search?

Всем привет, ребята выручайте!!!!!!!!!!!!!!!!!!!!!!!

Не работает фильтр в model search, не фильтрует товары по выбранной категории.

http://sportpit.alex-sport.com.ua/category/index?_ТУТ_ДОЛЖЕН_БЫТЬ_ID_ProductPitSearch%5Bprice%5D=672%2C1032&ProductPitSearch%5Bbrand_id%5D=89

Bad Request (#400) Отсутствуют обязательные параметры: id

где надо прописать этот id, если я правильно понимаю, то id на передать в форму _search, если да то как??

ProductPitSearch

<?php

namespace common\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use yii\helpers\VarDumper;

/**
 * ProductPitSearch represents the model behind the search form about             `common\models\ProductPit`.
 */
class ProductPitSearch extends ProductPit
{
public $sort;

/**
 * @inheritdoc
 */
public function rules()
{
    return [
        [['id', 'status', 'new', 'hit', 'sale', 'stock', 'top_sales', 'top_ten', 'category_id', 'img_id', 'gallery_id', 'brand_id', 'country_id', 'quantity', 'quantity_product', 'packing', 'availability', 'created_at', 'updated_at'], 'integer'],
        [['title', 'content', 'composition', 'vendor_code', 'seo_keywords', 'seo_description'], 'safe'],
        //[['old_price', 'price'], 'number'],
        [['old_price', 'price'], 'string'],
    ];
}

/**
 * @inheritdoc
 */
public function scenarios()
{
    // bypass scenarios() implementation in the parent class
    return Model::scenarios();
}

/**
 * Creates data provider instance with search query applied
 *
 * @param array $params
 *
 * @return ActiveDataProvider
 */


public function search($params)
{
    //$query = ProductPit::find()->where(['category_id' => $_GET['id']]);

    $query = ProductPit::find();

    // add conditions that should always apply here

    $dataProvider = new ActiveDataProvider([
        'query' => $query,
    ]);

    $this->load($params);

    if (!$this->validate()) {
        // uncomment the following line if you do not want to return any records when validation fails
        // $query->where('0=1');
        return $dataProvider;
    }

    // grid filtering conditions
    $query->andFilterWhere([
        'category_id' => $this->id,
        'brand_id' => $this->brand_id,
        'country_id' => $this->country_id,
        'packing' => $this->packing,
    ]);


    if ($this->price) {
        $range = explode(',', $this->price);

        $query->andFilterWhere([
            'and',
            ['>', 'price', $range[0]],
            ['<', 'price', $range[1]]
        ]);
    }

    return $dataProvider;
}
}

CategoryController

public function actionIndex()
{
    $id = $_GET['id'];
    $searchModel = new ProductPitSearch();
 //        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

    $parentCat = CategoryPit::find()
        ->where(['parent_id' => $id])
        ->andWhere(['status' => CategoryPit::STATUS_CATEGORY_ON])
        ->asArray()
        ->all();

    $result = [];
    foreach ($parentCat as $cat) {
        $result[] = $cat['id'];
    }

    if ($id == isset($cat['parent_id'])) {
        $query = ProductPit::getAllProteinProduct($result);
    } else {
        $query = ProductPit::getProductByStatus($id);
    }

    $dataProvider = new ActiveDataProvider([
        'query' => $query,
        'pagination' => [
            'pageSize' => 12,
        ],
    ]);

    return $this->render('index', [
        'searchModel' => $searchModel,
        'dataProvider' => $dataProvider,
    ]);
}

_search

<?php $form = ActiveForm::begin([
    'action' => ['index', 'id' => $_GET['id']],
    'method' => 'get',

]); ?>
ЦЕНА
<br>
<br>
<br>
<?= $form->field($model, 'price')->widget(Slider::classname(), [
    'sliderColor' => Slider::TYPE_PRIMARY,
    'handleColor' => Slider::TYPE_PRIMARY,
    'pluginOptions' => [
        'tooltip' => 'always',
        'min' => $minPriceProduct,
        'max' => $maxPriceProduct,
        'step' => 1,

        'range' => true
    ],
])->label('');
Html::submitButton(Yii::t('app', 'OK'), ['class' => 'btn btn-primary'])
?>
<br>
<br>
Производитель
<br>
<br>
<?php
foreach ($getFilterManufacturesCheckbox as $key => $value) {
    if (!$value == null) {
        echo $form->field($model, 'brand_id')->checkbox(
            [
                'uncheck' => null,
                'label' => ProductPit::getManufacturesTitle($value),
                'value' => $value
            ]
        );
    }
}
Html::submitButton(Yii::t('app', 'OK'), ['class' => 'btn btn-primary'])
?>
<br>
<br>
Страна
<br>
<br>
<?php
foreach ($getFilterCountryCheckbox as $key => $value) {
    if (!$value == null) {
        echo $form->field($model, 'country_id')->checkbox(
            [
                'uncheck' => null,
                'label' => ProductPit::getCountryTitleRu($value),
                //'label' => $value,
                'value' => $value
            ]
        );
    }
}
Html::submitButton(Yii::t('app', 'OK'), ['class' => 'btn btn-primary'])
?>

<?php // echo $form->field($model, 'packing') ?>

<div class="form-group">
    <?= Html::submitButton(Yii::t('app', 'OK'), ['class' => 'btn btn-primary']) ?>
</div>

<?php ActiveForm::end(); ?>

UPDATE CODE!

Источник: https://ru.stackoverflow.com/questions/710989/%D0%9D%D0%B5-%D1%80%D0%B0%D0%B1%D0%BE%D1%82%D0%B0%D0%B5%D1%82-%D1%84%D0%B8%D0%BB%D1%8C%D1%82%D1%80-%D0%B2-model-search

Share

Тебе может это понравится...