创建此查询

时间:2015-01-21 20:38:31

标签: php yii

将createCommand隐藏到CDbCriteria。

如何使用 CDbCriteria 创建此查询?yii?

SELECT SUM(f.mablagh) as kol,k.code,k.name,m.name AS mName,m.code AS mCode
FROM T1 AS f
LEFT JOIN T2 AS k ON k.code=f.kCode
LEFT JOIN T3 As m ON m.code=k.main
GROUP BY k.main

T1型号:

public function relations()
{
    return array(
        'kCode0' => array(self::BELONGS_TO, 'T2', 'kCode'),
    );
}

T2型号:

return array(
       'main' => array(self::BELONGS_TO, 'T3', 'mymain'),
    );

换句话说,我想转换这段代码:

$result = Yii::app()->db->createCommand()
                ->select('SUM(f.mablagh) as kol,k.code,k.name,m.name AS mName,m.code AS mCode')
                ->from('T1 AS f')
                ->join('T2 AS k' ,'k.code=f.kCode')
                ->join('T3 AS m' ,'m.code=k.main')
                ->group('k.main')
                ->queryAll();

要:

$criteria   = new CDbCriteria;
.
.
.
.
$result = T1::model()->findAll($criteria);

1 个答案:

答案 0 :(得分:0)

如果您想使用标准,那么您可以在没有任何其他模型关系的情况下使用它:

$criteria   = new CDbCriteria;
$criteria->select = "SUM(t.mablagh) as kol,k.code,k.name,m.name AS mName,m.code AS mCode";
$criteria->join = "LEFT JOIN T2 AS k ON k.code=t.kCode LEFT JOIN T3 AS m ON m.code=k.main";
$criteria->group = "k.main";
$result = T1::model()->findAll($criteria);