php - Yii: How to refer to model's field when columns name duplication performing relational query? -
i try execute relational query in yii:
$r = machinedata::model()->with('machinegames')->findbypk($machine_id);
but returns error:
cdbcommand failed execute sql statement: sqlstate[42702]: ambiguous column: 7 error: column reference "machine_id" ambiguous line 1: ..."."game_id") ("t"."machine_id"=3) order machine_id...
it seems problem in order by
clause reference machine_id
unclear. may refer both of tables because both have machine_id
column. can suggest me solution, please? regards!
p.s. using following cdbcriteria gives same error:
$criteria=new cdbcriteria(); $criteria->alias = "u"; $criteria->compare('u.machine_id',$machine_id); $criteria->with = array('machinegames'); $r = machinedata::model()->findall($criteria);
this relation in model 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 }
this relation in model 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 }
i think problem lies in machinedata::relations() method:
public function relations() { return array('machinegames' => array(self::has_many, 'machinegames', 'machine_id', 'order'=>'machine_id', 'with'=>'game'); }
you should disambiguate *machine_id* here explained in docs cactiverecord::relations() :
public function relations() { return array('machinegames' => array(self::has_many, 'machinegames', 'machine_id', 'order'=>'machinegames.machine_id', 'with'=>'game'); }
nb : code above using relation's name, hence *machine_games.machine_id* column. if want disambiguate on main table column (here : *machine_data.machine_id*), use alias 't'.
Comments
Post a Comment