我尝试在 Yii 中执行这个关系查询:
$r = MachineData::model()->with('machineGames')->findByPk($machine_id);
但它返回此错误:
CDbCommand failed to execute the SQL statement: SQLSTATE[42702]: Ambiguous column: 7 ERROR: column reference "machine_id" is ambiguous
LINE 1: ..."."game_id") WHERE ("t"."machine_id"=3) ORDER BY machine_id...
似乎问题ORDER BY
出在引用machine_id
不清楚的条款中。它可能会引用这两个表,因为它们都有machine_id
列。你能给我建议任何解决方案吗?问候!
Ps 使用以下 CDbCriteria 会产生相同的错误:
$criteria=new CDbCriteria();
$criteria->alias = "u";
$criteria->compare('u.machine_id',$machine_id);
$criteria->with = array('machineGames');
$r = MachineData::model()->findAll($criteria);
这是模型中的关系MachineData
:
abstract class BaseMachineData extends GxActiveRecord {
public function relations() {
return array('machineGames' => array(self::HAS_MANY, 'MachineGames', 'machine_id', 'order'=>'machine_id', 'with'=>'game');
}
//code goes here
}
class MachineData extends BaseMachineData{
//code goes here
}
这是模型 MachineGames 中的关系:
abstract class BaseMachineGames extends GxActiveRecord {
public function relations() {
return array('machine' => array(self::BELONGS_TO, 'MachineData', 'machine_id');
}
//code goes here
}
class MachineGames extends BaseMachineGames
{
//code goes here
}