我可以强制Yii中使用特定的别名在生成的SQL [英] Can I force Yii to use particular alias in generated SQL

查看:222
本文介绍了我可以强制Yii中使用特定的别名在生成的SQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用我的检索记录的CActiveRecord 与()语句:

I'm retrieving my records using CActiveRecord with a with() statement:

Probes::model()->with(array
(
    'user',
    'results',
    'results.answer',
    'survey',
    'survey.questions'
))->findByPk($id)

我想用 GROUP BY survey.questions question_id 字段code>的关系,所以我在上面更改为:

I wanted to use GROUP BY on question_id field of survey.questions relation, so I changed above to:

'survey.questions'=>array('group'=>'survey.questions.question_id'),

这引起了SQL异常 1054未知列。但是,通过分析连接SQL code:

This caused a SQL exception 1054 Unknown column. However, by analyzing attached SQL code:

`questions`.`question_id` AS `t6_c2`

我设法找到了,我必须使用 t6_c2 别名(被Yii自动生成的?)。所以,再变:

I managed to find out, that I have to use t6_c2 alias (auto-generated by Yii?). So, another change to:

'survey.questions'=>array('group'=>'t6_c2'),

和问题解决。

不过,话又说回来,别名 t6_c2 似乎很......不稳定(自动生成的?)对我来说。我可以强制的Yii在这部分生成的SQL一些其他的别名,由我提供的?或者我如何一定能,这部分的SQL code不会在下次(稍后)产生变化?还是 - ?有没有实现任何其他方式,我想实现

But, then again, alias t6_c2 seems quite... "unstable" (auto-generated?) for me. Can I force Yii to in this part of generated SQL some other alias, provided by me? Or how certain can I be, that this part of SQL code won't change upon next (some later) generation? Or -- is there any other way to achieve, what I want to achieve?

推荐答案

您可以指定别名,你的关系

you can assign alias to your relation

,
'survey.questions'=>array(
    'alias' => 'surq'
    'group'=>'surq.question_id',
),

阅读和的这个了解更多信息

这篇关于我可以强制Yii中使用特定的别名在生成的SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆