2

我的代码:

$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;

笔记:

1-userProfiles关系在用户模块中定义

问题:

尽管提供了选择属性,但它仅从 User 表而不是从 user_profiles 表加载数据'select'=>'firstname'

我想要来自useruserProfile表的数据。

4

1 回答 1

1

关系self::HAS_ONE不能和主查询一起执行,试试join

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

像这样的东西,但你应该在用户 AR 中创建名字字段来检索它。

或更改与 HAS_MANY 的关系并检查数组的第一个实体。

together:与此关系关联的表是否应强制与主表和其他表连接在一起。此选项仅对 HAS_MANY 和 MANY_MANY 关系有意义。

因为您不能编写为每个加入记录只提供一条记录的查询

SELECT * FROM main
INNER JOIN slave ON (slave.main_id = main.id limit 1) // will not work);
于 2014-04-28T15:00:29.897 回答