简体   繁体   English

在Yii上使用FindAll,字段名称无效

[英]FindAll on Yii, invalid field name

I am using YII 1.1 and trying to create an relationship between 3 tables. 我正在使用YII 1.1并尝试在3个表之间创建关系。

So far so good, but when i try to do a where clausule inside the findAll the stuffs get a litte weird. 到目前为止,一切都很好,但是当我尝试在find里面做一个地方时,所有的东西都会变得有点奇怪。

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. 问题是,生成的sql语句告诉我已激活的列不存在。

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". 但是确实如此,正如我在代码上看到的那样,Yii为课程创建了一个别名,别名为“ t”。

If i change the findAll and use t.activated instead of course.activated it works like a charm. 如果我更改find​​All并使用t.activated而不是course.activated,它的工作原理就像一个魅力。 But, it does not feels right, how can I use course instead of the "t" alias that the yii used on the query? 但是,感觉不对,如何使用课程代替yii在查询中使用的“ t”别名? If I remove the t., of course the mysql tells me that the activated is ambiguous 如果我删除了t。,当然mysql告诉我激活的是不明确的

By default Yii alias the primary table in the SQL query with "t", but it doesn't mean you have to stick with it. 默认情况下,Yii在SQL查询中的主表中使用“ t”作为别名,但这并不意味着您必须坚持使用。 You can change it by setting "alias" property of CDbCriteria 您可以通过设置CDbCriteria的“ alias”属性来更改它

$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. 条件参数接受CDbCriteria对象,类似数组的CDbCriteria的属性或字符串。

http://www.yiiframework.com/doc/api/1.1/CDbCriteria#alias-detail http://www.yiiframework.com/doc/api/1.1/CDbCriteria#alias-detail

http://www.yiiframework.com/doc/api/1.1/CActiveRecord#findAll-detail http://www.yiiframework.com/doc/api/1.1/CActiveRecord#findAll-detail

Thanks to SiZE I found on the yii the following text: 多亏了SiZE,我在yii上找到了以下文本:

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. 在关系AR查询中,默认情况下,主表的别名固定为t,而关系表的别名与对应的关系名称相同。

So, as SiZE told, the t is always the alias for the primary table. 因此,正如SiZE所说,t始终是主表的别名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM