I tried to do this:
$dataProvider=new CActiveDataProvider('User', array(
'criteria'=>array(
'alias'=>'{{users}}',
'join'=>'LEFT JOIN {{user_network}} as un ON un.user_id = {{users}}.id',
'condition'=>'un.network_id = ' . $id,
),
));
However, I received an error that complained of the ambiguity of “id” in my SELECT. This was because both users and user_network had an id column, and the SELECT statement didn’t clarify. Since this ambiguous “id” in the select came from the default scope (and I couldn’t add the alias into the default scope, since the alias might be different for different queries), this is what I did as a workaround:
$dataProvider=new CActiveDataProvider(User::model()->resetScope()->joinsafe(), array(
'criteria'=>array(
'alias'=>'{{users}}',
'join'=>'LEFT JOIN {{user_network}} as un ON un.user_id = {{users}}.id',
'condition'=>'un.network_id = ' . $network->id,
),
));
where joinsafe was a new scope in User that I defined that disambiguated the id column to use this specific alias ("{{users}}"):
public function scopes()
{
$table = $this->tableName();
return array(
'joinsafe'=>array(
'select' => "$table.id, $table.email, $table.createtime, $table.lastvisit, $table.superuser, $table.status",
),
);
}
This seems terribly ugly and counterintuitive. Is there something I could have done instead?