My code :

$criteria = new CDbCriteria();

//  $criteria->select = array("id");

$criteria->with = array('userProfiles'=>array(
    'select'=>'firstname'
));
$criteria->together = true;

$criteria->condition = "firstname like '%$q%' "
        . "and (user_id not in (select id2 from friends where id1=$user_id) "
        . "and user_id not in (select id1 from friends where id2=$user_id))";

$dataProvider = new CActiveDataProvider("Users", array(
    'criteria' => $criteria
));

return $dataProvider;

Notes:

1- userProfiles relation is defined in User module

Issue:

It is loading data only from User table not also from the user_profiles table despite of providing select atribute 'select'=>'firstname'

I want data from both user and userProfile table.

有帮助吗?

解决方案

Relation self::HAS_ONE can't be executed together with main query, try use join

$criteria->select = "*, user_profiles.firstname firstname"; $criteria->join = 'user_profiles ON user_profiles.user_id = users.id';

Something like this, but you should create firstname field in User AR to retrieve it.

Or change relation to HAS_MANY and check first entity of array.

together: whether the table associated with this relationship should be forced to join together with the primary table and other tables. This option is only meaningful for HAS_MANY and MANY_MANY relations.

Because you can't write query that will give you only one record for each joining record

SELECT * FROM main
INNER JOIN slave ON (slave.main_id = main.id limit 1) // will not work);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top