I've installed yii 1.0.5 and got some troubles with defaultScope and relations:
I have 2 tables - Question and Category - Question has field CategoryId which points to parent Category.
In models\Question.php I have below code:
class Question extends CActiveRecord { ... public function relations() { return array( 'Category'=>array(self::BELONGS_TO, 'Category', 'CategoryId'), ... }
In models\Category.php
class Category extends CActiveRecord { ... public function defaultScope() { return array( 'condition'=>'LanguageId="'.Yii::app()->language.'"', ); } ... }
I received next error in SQL:
SELECT `Question`.`Id` AS t0_c0, t1.`Id` AS t1_c0, t1.`ParentId` AS t1_c1, t1.`Level` AS t1_c2, t1.`SomeId` AS t1_c3, t1.`Name` AS t1_c4, t1.`Description` AS t1_c5, t1.`LanguageId` AS t1_c6, t1.`Flags` AS t1_c7 FROM `Question` LEFT OUTER JOIN `Category` t1 ON (`Question`.`CategoryId`=t1.`Id`) AND (LanguageId="en_us") WHERE (`Question`.`Id` IS NULL)
Exception 'CDbException' with message 'CDbCommand failed to execute the SQL statement: SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'LanguageId' in on clause is ambiguous'
Then I've changed defaultScope as below:
public function defaultScope() { return array( 'condition'=>'??.LanguageId="'.Yii::app()->language.'"', ); }
Previous error was disappeared but new is arisen in SQL:
SELECT * FROM `Category` WHERE (??.LanguageId="en_us") AND (id=1) LIMIT 1
exception 'CDbException' with message 'CDbCommand failed to execute the SQL statement: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '??.LanguageId="en_us") AND (id=0) LIMIT 1' at line 1'
Looks like criteria can't work with "??" placeholder. Is this a bug?