如何在yii2中构建一个Query对象?

时间:2017-11-23 06:26:03

标签: php mysql yii2 yii2-advanced-app

我如何在yii2中将该对象与activedataprovider一起用于gridview小部件?

SELECT `activated_promo`.*
    ,rides_promo_count_view.count
FROM `activated_promo`
LEFT JOIN `promo_code` `promo` ON `activated_promo`.`promo_id` = `promo`.`id`
LEFT JOIN `users` `customer` ON `activated_promo`.`userID` = `customer`.`ID`
LEFT JOIN rides_promo_count_view ON promo.id = rides_promo_count_view.promo_id

1 个答案:

答案 0 :(得分:0)

假设您有每个mysql表的ActiveRecord类,您可以使用以下查询。

    //By Assuming you have Active Record for each table
    $activedPromo = ActivitedPromo::find()
        ->select('activated_promo.*','rides_promo_count_view.count')
        ->leftJoin('`promo_code` `promo`', '`activated_promo`.`promo_id` = `promo`.`id`')
        ->leftJoin('`users` `customer`', '`activated_promo`.`userID` = `customer`.`ID`')
        ->leftJoin('rides_promo_count_view', 'promo.id = rides_promo_count_view.promo_id')
        ->all();

以上方式是其中之一,对于替代方案(例如使用joinWith),您可以查看YII ActiveRecord

中的文档