建立一个特定的模型关系YII activerecord

时间:2013-01-11 05:46:53

标签: activerecord yii models yii-relations

我正在尝试使用CGridView显示Vehicle模型。

为了显示Fld7465RRef参考列的值,需要以下sql select:

select VUF._Fld7468_S as Loading_Time_To_DLR  
FROM Vehicles as Vehicles  
left join (_InfoReg7464 as VUF                 
inner join _Chrc7246 as CFU
on VUF._Fld7467RRef = CFU._IDRRef                  
and CFU._Description ='Vehicle uploading for DLRTime')                 
on Vehicles._IDRRef =  VUF._Fld7465RRef

我找不到为此查询构建关系的解决方案。

1 个答案:

答案 0 :(得分:2)

一种方法是DAO

$mySqlString = "
    select VUF._Fld7468_S as Loading_Time_To_DLR  
    FROM Vehicles as Vehicles  
    left join (_InfoReg7464 as VUF                 
    inner join _Chrc7246 as CFU
    on VUF._Fld7467RRef = CFU._IDRRef                  
    and CFU._Description ='Vehicle uploading for DLRTime')                 
    on Vehicles._IDRRef =  VUF._Fld7465RRef
";
$command = Yii::app()->db->createCommand($mySqlString); 
$aResult = $command->query()->readAll();

当然,您应该使用如下语句绑定您的参数(如果有的话):

$command->bindParam(":userID", $userID, PDO::PARAM_STR);

另一种方式是query-builder