I now want to get the count of unique records. There is another column along with activity_type named ‘target_id’. And the result should be the count of rows, where ‘activity_type’=‘suspended’ and ‘target_id’ is unique i.e. the rows should have the same value in their ‘target_id’ column.
How can this be achieved with the relations() method i.e. without having to make write method to do that?
This gives me unexpected results. If, for example, there are 3 rows with ‘activity_type=suspended’, two of them have their ‘target_id=foo’ and one of them has ‘target_id=bar’. I should get ‘2’ as the answer when I output ‘$object->ignoredEmails’. In the example that you provided, I’m getting ‘1’.
If all the three have the same ‘target_id’ then the answer should be ‘1’. If all the three have different ‘target_id’, the answer should be ‘3’.
I’m looking to try something with ‘SELECT DISTINCT …’
I was using ‘SELECT DISTINCT target_id’ in a separate function earlier and doing ‘count()’ on the array returned by CDbCommand->readAll(). My question is, using relations(), why do I get an error if I use only ‘select’ => ‘DISTINCT target_id’ instead of ‘select’ => ‘count( DISTINCT target_id )’, since I’m already using ‘self::STAT’? Is my question clear?
Because the STAT relation needs to have a “count” select at the executed SQL. DISTINCT target_id would return all the rows with the unique target_id instead of the numbers of the rows. If you see what I mean .