Model Search

Hi guys, I want to ask, how can I pass data to a model search and filter on field with this data. I have the following model,


<?php


namespace app\modules\menus\models;


use Yii;

use yii\base\Model;

use yii\data\ActiveDataProvider;

use app\modules\menus\models\MenuCategories;


/**

 * MenuCategoriesSearch represents the model behind the search form about `app\modules\menus\models\MenuCategories`.

 */

class MenuCategoriesSearch extends MenuCategories

{

    public function rules()

    {

        return [

            [['id', 'menu'], 'integer'],

            [['name', 'category_ids'], 'safe'],

        ];

    }


    public function scenarios()

    {

        // bypass scenarios() implementation in the parent class

        return Model::scenarios();

    }


    public function search($params)

    {

        $query = MenuCategories::find();


        $dataProvider = new ActiveDataProvider([

            'query' => $query,

        ]);


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

            return $dataProvider;

        }


        $query->andFilterWhere([

            'id' => $this->id,

            'menu' => $this->menu,

        ]);


        $query->andFilterWhere(['like', 'name', $this->name])

            ->andFilterWhere(['like', 'category_ids', $this->category_ids]);


        return $dataProvider;

    }

}

but I want to select only those results that have field name = 1 or name= 0 … depending on what I need? In the previous version, these data are fed into the grid

if you want to use = operator instead of like you have to adjust the following line:


$query->andFilterWhere(['like', 'name', $this->name])

to be


$query->andFilterWhere(['name' => $this->name])

See also the guide on building query conditions: http://www.yiiframework.com/doc-2.0/guide-query-builder.html#where

Okay, but I want to do another. How can I submit a parameter from the controller to the model value of the name and then take only those records that meet that value

When you called the search method in controller you get the dataprovider with the query. You can then modify the query to meet your condition:




$dataProvider = $searchModel->search($_GET);


$dataProvider->query->andWhere(['name' => 1]);



Well, it can be done in the model itself, because the data is taken from $_GET -> and $params.

I have one more question, why in the alpha version, when you generate search model with gii us create the following cod


 public function search($params)

    {        

        $query = CatalogueItems::find();

        $dataProvider = new ActiveDataProvider([

            'query' => $query,

        ]);

        

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

            return $dataProvider;

        }


        $this->addCondition($query, 'media_data');

        $this->addCondition($query, 'media_data', true);

        $this->addCondition($query, 'type');

        $this->addCondition($query, 'tags');

        $this->addCondition($query, 'tags', true);

        $this->addCondition($query, 'title', true);

        $this->addCondition($query, 'summary');

        $this->addCondition($query, 'summary', true);

        $this->addCondition($query, 'body');

        $this->addCondition($query, 'body', true);

        $this->addCondition($query, 'code');

        $this->addCondition($query, 'code', true);

        $this->addCondition($query, 'seo_title');

        $this->addCondition($query, 'seo_title', true);

        $this->addCondition($query, 'seo_description');

        $this->addCondition($query, 'seo_description', true);

        $this->addCondition($query, 'seo_keywords');

        $this->addCondition($query, 'seo_keywords', true);

        

        return $dataProvider;

    }


    protected function addCondition($query, $attribute, $partialMatch = false)

    {

        $value = $this->$attribute;

        if (trim($value) === '') {

            return;

        }

        if ($partialMatch) {

            $value = '%' . strtr($value, ['%'=>'\%', '_'=>'\_', '\\'=>'\\\\']) . '%';

            $query->andWhere(['like', $attribute, $value]);

        } else {

            $query->andWhere([$attribute => $value]);

        }

    }

And in the new version it does not have these methods addCondition?

because in the new version we added filterWhere() method to the query which basically does what addCondition did before.