I’m trying to fix old projects that have been affected by the SQL db alias change noted here http://code.google.com/p/yii/issues/detail?id=796
But defaultScope doesn’t seem to do what I expect it to in all cases.
I am getting:
CDbCommand failed to execute the SQL statement: SQLSTATE[42S22]: Column not found:
1054 Unknown column 'Tag.id' in 'on clause'
Here’s the start of the Model with the defaultScope as I understand it should be :
class Tag extends CActiveRecord {
public static function model($className=__CLASS__) { return parent::model($className); }
public function tableName() { return 'Tag'; }
public function defaultScope() {
return array(
'alias' => 'Tag',
);
}
}
And later in the same class I call the following, which generates the error:
public function findTagWeights($limit = 20) {
$criteria = new CDbCriteria(array(
'select' => 'name, COUNT(postId) as weight',
'join' => 'INNER JOIN PostTag ON Tag.id = PostTag.tagId',
'group' => 'name',
'having' => 'COUNT(postId) > 0',
'order' => 'weight DESC',
'limit' => $limit
));
$rows = $this->dbConnection->commandBuilder->createFindCommand($this->tableSchema, $criteria)->queryAll();
If I change Tag.id to t.id, all works. I don’t understand why. Please educate me. Thanks.