Your relation is inverted. Each User
can have many UserSite
s therfore your relation should be
'userSites' => array(self::HAS_MANY, 'UserSite', 'user_id')
To eager load relations in CActiveDataProvider
you have to set CDbCriteria::together
to true
:
if($sid!=null){
$criteria->together = true;
...
An explanation for this is given at http://www.yiiframework.com/wiki/280/1-n-relations-sometimes-require-cdbcriteria-together/
But tracing the application's queries shows that when CActiveDataProvider gets data, it handles 1:n related tables a bit different. AR gets the data from the parent table and 1:1 related child tables with one query joining all those 1:1 child tables. 1:n child tables are not part of this query. AR gets data from those with subsequent queries, one query per 1:n child table, specifying the rows it wants with a WHERE PK IN (id1, id2, ...) clause.