public function getParent()
{
return $this->hasOne(self::className(), ['categories_id'=>'parent_id'])->from(self::tableName().' parent');
}
And in my ModelSearch I have added
$query->joinWith(['parent']);
But when I try to filter any data it throws an exception like :
SQLSTATE[23000]: Integrity constraint violation: 1052 Column ‘categories_id’ in where clause is ambiguous The SQL being executed was: SELECT COUNT(*) FROM categories LEFT JOIN categories parent ON categories.parent_id = parent.categories_id WHERE categories_id=‘23’
Where do I put the table alias for categories_id in the WHERE statement.
@dannythebestguy… I just see you also reported this on my posted wiki for gridview filtering.
Just check the search() function in your model that returns the dataProvider. I have not personally tried this. But can you check and let know?
First we need to alias your existing table query. Not sure how to do this with find(). Can you change to using findBySQL instead of find (to alias your table name - e.g. child).
$sql = 'SELECT * FROM tbl_person AS child';
$query = Person::findBySQL($sql);
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
You may need to change all attribute columns to have aliases. Therefore change your addCondition statements:
$this->addCondition($query, 'child.categories_id');
// any other conditions
The error is in your model relation. Just change this and it should work (you need to alias your categories_id column as shown below).
public function getParent()
{
return $this->hasOne(self::className(),
['parent.categories_id' => 'parent_id'])->
from(self::tableName().' AS parent');
}
No Kartik, it still throws the error when I put some value on the Grid filter.
[b]SQLSTATE[23000]: Integrity constraint violation: 1052 Column ‘categories_id’ in where clause is ambiguous
The SQL being executed was: SELECT COUNT(*) FROM categories LEFT JOIN categoriesparent ON categories.parent_id = parent.categories_id WHERE categories_id=‘15’[/b]