最新消息: 新版网站上线了!!!

yii把复杂的sql转成ar

         SELECT A. * , (
SELECT COUNT( B.AdsId ) 
FROM Assigned AS B
WHERE B.AdsId = A.AdsId
AND A.ClientId =1
)
FROM Ads AS A
WHERE A.ClientId =1
LIMIT 0 , 30
 
模型
public function relations(){
     return array(
        ...
       'AdsIdCount'=> array(self::STAT,'Ads','AdsId'),
       //'VarName'=>array('RelationType', 'ClassName', 'ForeignKey', ...additional options)
 
     );
}
 
 
$result = Assigned::model()->find($criteria); 
 
$result->adsIdCount ; // do logic here
or
 
$results = Assigned::model()->findAll($criteria); 
foreach ($results as $result){
    $result->adsIdCount ; // do logic here
}

转载请注明:谷谷点程序 » yii把复杂的sql转成ar