我越来越陷入yii的活跃记录。
我在我的数据库中为每个表使用模型。
我尝试获取的sql是从3个表中查询和加入。
我想要完成的是等同于sql命令:
SELECT location_code as stopkey, bay_no bay_no, description stop_name, route_area_code route_area_code, latitude latitude, longitude, build_code build_code, message_time message_time, ip_address ip_address, route.route_code route, make make, last_impact last_impact, impact_count impact_count, last_bootup last_bootup, bootup_count bootup_count, last_active_hour last_active_hour, last_active_day last_active_day, operator.operator_code operator_code, routes routes, bearing
FROM snapshot_stop_status route
JOIN route_visibility ON route.route_id = route_visibility.route_id
JOIN operator ON operator.operator_id = route_visibility.operator_id
WHERE usernm = 'me'
ORDER BY location_code
这是我到目前为止所尝试的内容:
public function relations() {
return array(
'RoutesVisibility' =>array(self::MANY_MANY,'route_visibility','route_id'),
);
}
如何使用模型中的关系函数来实现这一目标?
任何形式的帮助将不胜感激
答案 0 :(得分:1)
route_visibility是将运算符绑定到snapshot_stop_status的连接表。所以在yii中你可以指定执行关系的表:
'operators' =>array(self::MANY_MANY,'Operator','route_visibility(route_id,operator_id )'),
在运营商:
'routes' =>array(self::MANY_MANY,'Route','route_visibility(operator_id, route_id)'),
然后你可以请求:
$criteria = new CDbCriteria;
$criteria->with = array('operators' => array('condition' => 'usernm = "me"'));
$models => Route:model()->findAll($criteria);