Answer the question
In order to leave comments, you need to log in
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'
public function getCategory()
{
return $this->hasOne(Category::className(), ['id' => 'category_id']);
}
public function getCoupons()
{
return $this->hasMany(Coupon::className(), ['category_id' => 'id']);
}
[
'attribute' => 'category_id',
'value' => 'category.title',
],
$query->joinWith('category');
$query->andFilterWhere(['like', 'category.title', $this->category_id]);
<?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
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 questionAsk a Question
731 491 924 answers to any question