Domanda

How can I select only distinct records, from relational table, when using with() operator in Yii?

I'm getting my models (records) like that:

$probe = Probes::model()->with(array
(
    'user',
    'results',
    'results.answer',
    'survey',
    'survey.questions',
    'survey.questions.question',
    'survey.questions.question.answers',
    'manager'
))->findByPk($id);

I want to make sure, that survey.questions relation returns only distinct records. But it seems, that I don't see any way to achieve this (or I'm blind / not educated enough).

When giving relational table name / alias as array:

'results.question'=>array('alias'=>'results_question'),

the distinct key is not among those, that can be used in such array (as modifier).

I tried very ugly, bumpy way of changing select from default * to DISTINCT *:

'survey.questions'=>array('select'=>'distinct'),

But this has (of course?) failed:

Active record "SurveysQuestions" is trying to select an invalid column "distinct". Note, the column must exist in the table or be an expression with alias.

How can I achieve this (seemed so obvious and easy), if it is possible at all this way (using with())? If not, then -- please, advice how to get distinct records in relational table any way (other than manually filtering results using foreach, what I'm doing right now, and what is ugly).

È stato utile?

Soluzione

You could set CDbCriteria::distinct to true:

'survey.questions'=>array('distinct'=>true),
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top