yii查询CActiveDataProvider上的4个表

时间:2014-02-28 14:51:31

标签: mysql yii cactivedataprovider

嗨掌握了我的4张桌子

-pegawai    -penilaian    -universitas    -jurusan

我通常使用

    SELECT  u.nama_univ, j.singkatan, peg.* FROM pegawai AS peg LEFT JOIN penilaian AS pen ON pen.no_test=peg.no_test LEFT JOIN universitas AS u ON u.id=peg.univ_s1 LEFT JOIN jurusan AS j ON j.id=peg.bidang_s1

如何将此查询转换为yii?

我已尝试过此代码,但效果不佳

$dataProvider = new CActiveDataProvider('Pegawai', array(
    'criteria' => array(
        'select' => array(
            '`pen`.*',
            '`u`.`nama_univ` AS `nama_univ`',
            '`j`.`singkatan` AS `singkatan`'
        ),
        'join' => 'JOIN `Penilaian` AS `pen` ON `pen`.`no_test` = `t`.`no_test`',
        'join' => 'JOIN `Universitas` AS `u` ON `u`.`id` = `t`.`no_test`',
        'join' => 'JOIN `Jurusan` AS `j` ON `j`.`id` = `t`.`no_test`',
    )
));

但只有一个人加入

2 个答案:

答案 0 :(得分:0)

做一件事就是打印标准对象,如

$dataProvider = new CActiveDataProvider('Pegawai', array(
    'criteria' => array(
        'select' => array(
            '`pen`.*',
            '`u`.`nama_univ` AS `nama_univ`',
            '`j`.`singkatan` AS `singkatan`'
        ),
        'join' => 'JOIN `Penilaian` AS `pen` ON `pen`.`no_test` = `t`.`no_test`',
        'join' => 'JOIN `Universitas` AS `u` ON `u`.`id` = `t`.`no_test`',
        'join' => 'JOIN `Jurusan` AS `j` ON `j`.`id` = `t`.`no_test`',
    )
));
echo "<pre>";
print_r($dataProvider);   //Check the object or try to post it below
exit();

答案 1 :(得分:0)

您已为同一个数组索引('join')分配了3个值,因此只存储了最后一个。

尝试:

$dataProvider = new CActiveDataProvider('Pegawai', array(
'criteria' => array(
    'select' => array(
        '`pen`.*',
        '`u`.`nama_univ` AS `nama_univ`',
        '`j`.`singkatan` AS `singkatan`'
    ),
    'join' => 'JOIN `Penilaian` AS `pen` ON `pen`.`no_test` = `t`.`no_test` JOIN `Universitas` AS `u` ON `u`.`id` = `t`.`no_test` JOIN `Jurusan` AS `j` ON `j`.`id` = `t`.`no_test`',
)
));