I have now the same problem, when extending my model classes with relations.
My problem is that 4 or more columns have the same name.
Is there really need to add the table name to the columns of the selecting model class?
I mean everything worked with
<th><?php echo $sort->link('ID'); ?></th>
<th><?php echo $sort->link('Phone'); ?></th>
etc. if no related table has the same column names.
But if I add a relation to my model where the othertable has also the columns ID and Phone I have to write:
<th><?php echo $sort->link('table1.ID'); ?></th>
<th><?php echo $sort->link('table1.Phone'); ?></th>
And add the attributes to the CSORT object
$sort->attributes=array(
'table1.ID'=>'ID',
'table1.Phone'=>'Phone',
'othertable.somecolumn'=>'somecolumn'
);
I understand that if I want a specific column of a related table that I have to add the alias which is set in the relation function of the model.
But I don't understand why its needed to add the tablename for the own tablecolumns of the model (e.g. if ID is also a column of the releated table).
Why is there no "default" alias used in the query (which introduce the Column 'ID' in order clause is ambiguous error).
I mean it should be easy to change the sql to
select * from table1 as default LEFT OUTER JOIN `othertable` othertablealias ON (`table1`.`foreignkey`=othertablealias.`ID`) ORDER BY default.`ID` LIMIT 10
or either the model table name itself if nothing else is set e.g.
ORDER BY `table1`.`ID`
Cause I have created all the sites with the crud command and extending now model after model with relations.
And therefore I have to add the "tablename" for the columns of the modelclass itself, which have worked before a relation has been added to the model.
this e.g. would do this mentioned above
public function applyOrder($criteria) in CSort
...
$default_table=CActiveRecord::model($this->modelClass)->tableName(); // add
foreach($directions as $attribute=>$descending)
{
if(($pos=strpos($attribute,'.'))!==false)
$attribute=$schema->quoteTableName(substr($attribute,0,$pos)).'.'.$schema->quoteColumnName(substr($attribute,$pos+1));
else
$attribute=$schema->quoteTableName($default_table).'.'.$schema->quoteColumnName($attribute); // change
//$attribute=$schema->quoteColumnName($attribute); // original
$orders[]=$descending?$attribute.' DESC':$attribute;
}
...
But I am not sure if everything is working as expected in every case.
Any Opinions?