I'm retrieving my records using CActiveRecord
with a with()
statement:
Probes::model()->with(array
(
'user',
'results',
'results.answer',
'survey',
'survey.questions'
))->findByPk($id)
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'),
This caused a SQL exception 1054 Unknown column
. However, by analyzing attached SQL code:
`questions`.`question_id` AS `t6_c2`
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'),
and the problem is solved.
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
read this and this for more info
You can set specific and unique alias for each relation table in a relations method in your model. For example,
"user"=>array(self::HAS_MANY, "User", "user_id", "alias"=>"your_alias_for_this_relation")
Try this (i'm asumming 'survey.questions' is a field of the table probe)