V
V
Victor Umansky2017-08-26 19:04:23
Yii
Victor Umansky, 2017-08-26 19:04:23

Filter not working in model search?

The filter in model search does not work, does not filter products by the selected category.

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

Bad Request (#400)
Required parameters missing: id

where it is necessary to register this id, if I understand correctly, then the id should not be transferred to the _search form, if so, how ??
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]]
            ]);
        }

//        $query->andFilterWhere(['like', 'brand_id', $this->brand_id])
//            ->andFilterWhere(['like', 'country_id', $this->country_id])
//            ->andFilterWhere(['like', 'packing', $this->packing]);

        return $dataProvider;
    }
}

CategoryController
public function actionIndex($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'],
        '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(); ?>

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
M
Maxim Timofeev, 2017-08-27
@Uman

Here is the action specified in your form,
and here is the action itself The action
requires an id, but there is none in the form
Therefore, either make the action independent of the id:
or
or add the id to the form, for example:
It is difficult to say exactly how it is correct without understanding the whole picture, but I think the logic is clear.

B
Boris Korobkov, 2017-08-27
@BorisKorobkov

public function actionIndex($id)

This action requires a mandatory $id parameter. If it is not specified, then it will be:
Bad Request (#400)
Required parameters missing: id

Accordingly, you must either always pass it, or initiate it with a default value (for example, the root category)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question