On my Yii2 (c21895d4dd4c18d5bab37e0b7b359668f8aa8b67) this will output error Column 'name' in field list is ambiguous. So I have to put something like this
Do I really have to do this for every query like this? Or is there any simpler way I don't know?
I'm thinking that if I can disambiguate the table name right from the addSelect method, it would be much easier. I extend the ActiveQuery and do something like this.
private $_tableName;
private function getTableName() {
//since the `from` will be resolved in the `prepare` method.
if (!isset($this->_tableName)) {
$class = $this->modelClass;
$this->_tableName = $class::tableName();
}
return $this->_tableName;
}
public function addSelect($columns) {
if (is_array($columns)) {
$columns = array_map(function($column) {
return (strpos($column, ".") == false) ? $this->getTableName() . ".{$column}" : $column;
}, $columns);
} else {
$columns = (strpos($column, ".") == false) ? $this->getTableName() . ".{$columns}" : $columns;
}
return parent::addSelect($columns);
}
I have several tables that have columns with same name. Let's say
shelf
,book
,chapter
, have the same fieldname
.I have a page that will show list of
chapter
that will also show the name of thebook
as well as theshelf
.Naturally this is the query I used for the
ActiveDataProvider
But I only wanted to show the name in the
GridView
, so this is what I do to avoid theSELECT *
.On my Yii2 (c21895d4dd4c18d5bab37e0b7b359668f8aa8b67) this will output error
Column 'name' in field list is ambiguous
. So I have to put something like thisDo I really have to do this for every query like this? Or is there any simpler way I don't know?
I'm thinking that if I can disambiguate the table name right from the
addSelect
method, it would be much easier. I extend theActiveQuery
and do something like this.