SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'category_id' in where clause is ambiguous
The SQL being executed was: SELECT COUNT(*) FROM `product` `p` LEFT JOIN `product` `pp` ON `p`.`id` = `pp`.`category_id` WHERE (`p`.`category_id`='1') AND (`category_id` IN ('6', '7', '8'))
public function search($params)
{
$query = Product::find()
->from(['p' => Product::tableName()])
->with(['designer', 'category'])
->joinWith([
'childrenProducts pp' => function (ActiveQuery $query) {
if (!empty($this->category_id)) {
$ids = [$this->category_id];
$childrenIds = $ids;
while ($childrenIds = Category::find()
->alias('p2')
->select('id')
->andWhere(['parent_id' => $childrenIds])
->column()) {
$ids = array_merge($ids, $childrenIds);
}
$query->andWhere(['category_id' => array_unique($ids)]);
}
}
]);
// 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([
'p.id' => $this->id,
'p.category_id' => $this->category_id,
'p.designer_id' => $this->designer_id,
'p.price' => $this->price,
'p.new' => $this->new,
'p.hit' => $this->hit,
'p.sale' => $this->sale,
'p.created_at' => $this->created_at,
'p.updated_at' => $this->updated_at,
'pp.childrenProducts' => $this->childrenProducts
]);
SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'category_id' in where clause is ambiguous
The SQL being executed was: SELECT COUNT(*) FROM `product` `p` LEFT JOIN `product` ON `p`.`id` = `product`.`category_id` WHERE (`p`.`category_id`='1') AND (`category_id` IN ('1', '6', '7', '8'))
SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias: 'product'
The SQL being executed was: SELECT COUNT(*) FROM `product` LEFT JOIN `product` ON `product`.`id` = `product`.`category_id` WHERE (`category_id`='1') AND (`category_id` IN ('1', '6', '7', '8'))