A
A
Anton Natarov2016-07-09 17:28:03
This
Anton Natarov, 2016-07-09 17:28:03

Yii2 Search Model is not a known column, what am I doing wrong?

Good afternoon. I work with Gridview and its search model.
actually I have two models one to many, categories with a coupon.
Mistake

SQLSTATE[23000]: Integrity constraint violation: 1052 
Column 'id' in where clause is ambiguous
The SQL being executed was: 
SELECT COUNT(*) FROM `rcq_coupon` 
LEFT JOIN `rcq_category` 
ON `rcq_coupon`.`category_id` = `rcq_category`.`id` WHERE `id`='1'

connections
public function getCategory()
    {
        return $this->hasOne(Category::className(), ['id' => 'category_id']);
    }

public function getCoupons()
    {
        return $this->hasMany(Coupon::className(), ['category_id' => 'id']);
    }

in the grid, everything outputs normally if you use this construction
in columns
[
                'attribute' => 'category_id',
                'value' => 'category.title',
            ],

in the SearchModel itself, I moved the category_id attribute to safe and added the following.
$query->joinWith('category');
$query->andFilterWhere(['like', 'category.title', $this->category_id]);

I have tables with a prefix, and if I specify the rcq_category.title prefix in andFilter,
then it works, but the search by Id and title of the coupon itself stops working.
the whole Search model if that
<?php

namespace backend\models\search;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\Coupon;

/**
 * CouponSearch represents the model behind the search form about `common\models\Coupon`.
 */
class CouponSearch extends Coupon
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'price', 'partner_id', 'counter_purchase', 'rating', 'status', 'created_at', 'updated_at'], 'integer'],
            [['title', 'description', 'category_id', 'conditions', 'acts_date', 'before_date'], 'safe'],
        ];
    }

    /**
     * @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 = Coupon::find();

        // add conditions that should always apply here

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

        $query->joinWith('category');

        $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([
            'id' => $this->id,
            'price' => $this->price,
            'partner_id' => $this->partner_id,
            'counter_purchase' => $this->counter_purchase,
            'rating' => $this->rating,
            'status' => $this->status,
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,
        ]);

        $query->andFilterWhere(['like', 'title', $this->title])
            ->andFilterWhere(['like', 'description', $this->description])
            ->andFilterWhere(['like', 'conditions', $this->conditions])
            ->andFilterWhere(['like', 'acts_date', $this->acts_date])
            ->andFilterWhere(['like', 'before_date', $this->before_date])
            ->andFilterWhere(['like', 'rcq_category.title', $this->category_id]);

        return $dataProvider;
    }
}

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
A
Anton Natarov, 2016-07-09
@HanDroid

Decided to do without the search field. brought out DropDown by a Leaf in the grid

[
                'attribute' => 'category_id',
                'value' => 'category.title',
                'filter' => \common\models\Category::getParentsList()
            ],

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question