[SOLVED] STAT Relation not based on primary key

Hi All,

I have a quick question, it involves two of my models, I have ‘Agent’ and ‘Property’. Each agent can have many properties, and I can count the number of properties they have in the Agent model easily by using:

public function relations()


    return array(


        'propertycount'=>array(self::STAT, 'Property', 'agent_id'),



However, each property has a postcode, and I need to count the number of unique postcodes an agent has, for example:


id      agent_id     postcode     ...

1       101          AB12

2       101          AB12

3       101          AB12

4       101          B34

In the above example, agent with ID 101 has 4 properties, but 2 postcodes. Does that make sense?



'postCodeCount' => array(self::STAT, 'Property', 'agent_id', 'select' => 'COUNT(DISTINCT postcode)'),

should work.

Syntax may differ depending on SQL dialect, the code above is for MySQL.

perfect, thanks a lot for that! :)