Reputation: 1768
I am using YII 1.1 and trying to create an relationship between 3 tables.
So far so good, but when i try to do a where clausule inside the findAll the stuffs get a litte weird.
My model is called Course and it has many Modules which has many Videos. On the find all I want to narrow down to only the Courses, Modules and Videos which are activated.
To do so I use the following code:
$courses=Course::model()->with(array(
'module' => array('joinType'=>'INNER JOIN', 'together'=>true),
'module.video' => array('joinType'=>'INNER JOIN', 'together'=>true),
))->findAll("language_id=2 && ==> **course.activated=1** <== && module.activated=1 && video.activated=1");
The problem is, the sql statement generated tells me that the activated column does not exists.
CDbCommand failed to execute the SQL statement: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'course.activated' in 'where clause'. The SQL statement executed was: SELECT `t`.`id` AS `t0_c0`, `t`.`name` AS `t0_c1`, `t`.`description` AS `t0_c2`, `t`.`language_id` AS `t0_c3`, `t`.`activated` AS `t0_c4`, `module`.`id` AS `t1_c0`, `module`.`course_id` AS `t1_c1`, `module`.`name` AS `t1_c2`, `module`.`description` AS `t1_c3`, `module`.`activated` AS `t1_c4`, `video`.`id` AS `t2_c0`, `video`.`module_id` AS `t2_c1`, `video`.`name` AS `t2_c2`, `video`.`description` AS `t2_c3`, `video`.`video_url` AS `t2_c4`, `video`.`position` AS `t2_c5`, `video`.`activated` AS `t2_c6` FROM `Course` `t` INNER JOIN `Module` `module` ON (`module`.`course_id`=`t`.`id`) INNER JOIN `Video` `video` ON (`video`.`module_id`=`module`.`id`) WHERE (language_id=2 && course.activated=1 && module.activated=1 && video.activated=1)
But it does, as i can see on the code, the Yii create an alias to the course and the alias is "t".
If i change the findAll and use t.activated instead of course.activated it works like a charm. But, it does not feels right, how can I use course instead of the "t" alias that the yii used on the query? If I remove the t., of course the mysql tells me that the activated is ambiguous
Upvotes: 0
Views: 90
Reputation: 41
By default Yii alias the primary table in the SQL query with "t", but it doesn't mean you have to stick with it. You can change it by setting "alias" property of CDbCriteria
$courses = Course::model()->with(array(
'module' => array('joinType'=>'INNER JOIN', 'together'=>true),
'module.video' => array('joinType'=>'INNER JOIN', 'together'=>true),
))->findAll(array('condition' => "language_id=2 && ==> **course.activated=1** <== && module.activated=1 && video.activated=1", 'alias' => 'course'));
The condition parameter accepts a CDbCriteria object, an array-like CDbCriteria's properties or a string.
http://www.yiiframework.com/doc/api/1.1/CDbCriteria#alias-detail
http://www.yiiframework.com/doc/api/1.1/CActiveRecord#findAll-detail
Upvotes: 2
Reputation: 1768
Thanks to SiZE I found on the yii the following text:
In relational AR query, the alias name for the primary table is fixed as t, while the alias name for a relational table is the same as the corresponding relation name by default.
So, as SiZE told, the t is always the alias for the primary table.
Upvotes: 0