在Yii2中使用DBquery从选择查询中添加值

时间:2019-10-13 10:06:42

标签: yii2

我想使用dbquery在我的选择查询.iam中添加值(字符串)。有人知道如何做到这一点吗?

    $tableA = (new \yii\db\Query())
    ->select("sm_wp.no_agenda,mfwp.nama_wp,sm_wp.jenis_surat, 
              sm_wp.nomor_surat, sm_wp.tgl_surat,sm_wp.perihal")
    ->from('sm_wp')
    ->leftjoin('mfwp', 'sm_wp.id_mfwp = mfwp.id')
    ;

从该查询中,我在网格视图中获得了该表

no_agenda nama_wp jenis_surat  nomor_surat  tgl_surat  perihal
---------  ------ -----------  -----------  ---------  -------
     1       xx       1            22       21/10/2019  bla

我希望桌子变成这样

    no_agenda kategori  nama_wp jenis_surat  nomor_surat  tgl_surat  perihal
    ---------  ------  -----------  -----------  ---------  -------  -------
         1       foo        xx       1            22       21/10/2019  bla                          
         2       foo        yy       1            22       21/10/2019  ble

我想在我的gridview中添加相同的值(foo).thx

1 个答案:

答案 0 :(得分:0)

您需要使用\yii\db\Expression

对引号进行转义
$tableA = (new \yii\db\Query())
    ->select(
        [
            new Expression(
                'sm_wp.no_agenda,mfwp.nama_wp,sm_wp.jenis_surat,
                sm_wp.nomor_surat, sm_wp.tgl_surat,sm_wp.perihal,
                "foo" as kategori'
            ),
        ]
    )
    ->from('sm_wp')
    ->leftjoin('mfwp', 'sm_wp.id_mfwp = mfwp.id')