Yii CDbCriteria:如何从AR“第三”表中获取价值?

时间:2012-12-29 12:29:33

标签: yii

我有表:driver,driver_negotiation,base_negotiation和team

在DriverController模型中,我想获得驱动程序正在协商的team_id(位于base_negotiation中)。

在Driver模型中我有:

'driverNegotiation' => array(self::HAS_ONE, 'DriverNegotiation', 'driver_id')

在DriverNegotiation中:

'baseNegotiation' => array(self::BELONGS_TO, 'BaseNegotiation', 'base_negotiation_id')

在DriverController中我正在构建类似的东西:

$criteria=new CDbCriteria(array(
    'condition' => 'baseNegotiation.team_id=13',
    'with' => 'driverNegotiation',
    'together' => true,

));

但我明白了:

Column not found: 1054 Unknown column 'baseNegotiation.team_id' in 'where clause'. 
The SQL statement executed was: SELECT COUNT(DISTINCT `t`.`id`) FROM `driver` `t` 
LEFT OUTER JOIN `driver_negotiation` `driverNegotiation` ON 
(`driverNegotiation`.`driver_id`=`t`.`id`) WHERE (baseNegotiation.team_id=13) 

所以我可以从driver_nagotiation表中获取所有内容,但不能从一个表中获取任何内容。

我是否需要使用联接来获取正确的内容,还是有更好的方法?

1 个答案:

答案 0 :(得分:2)

'with' => 'driverNegotiation.baseNegotiation'

这就是诀窍。