Subquery in SELECT using Yii2 ActiveRecord

♀尐吖头ヾ 提交于 2021-02-10 05:54:06

问题


Is it possible to convert this kind of SQL into ActiveRecord query in Yii2:

SELECT 
*,
(select count(*) from pendaftar where pendaftar.prodi_pilihan_1 =  a.id_prodi_penerima)as jum1,
(select count(*) from pendaftar where pendaftar.prodi_pilihan_2 = a.id_prodi_penerima)as jum2 
FROM prodi_penerima as a

I have two relational models that are Pendaftar and ProdiPenerima.

This is Pendaftar model:

...
* @property ProdiPenerima $prodiPilihan1
* @property ProdiPenerima $prodiPilihan2
...
/**
 * @return \yii\db\ActiveQuery
 */
public function getPekerjaanIdPekerjaan()
{
    return $this->hasOne(Pekerjaan::className(), ['id_pekerjaan' => 'pekerjaan_id_pekerjaan']);
}

/**
 * @return \yii\db\ActiveQuery
 */
public function getUserPendaftar()
{
    return $this->hasOne(User::className(), ['id' => 'id_user_pendaftar']);
}

/**
 * @return \yii\db\ActiveQuery
 */
public function getProdiPilihan1()
{
    return $this->hasOne(ProdiPenerima::className(), ['id_prodi_penerima' => 'prodi_pilihan_1']);
}
/**
 * @return \yii\db\ActiveQuery
 */
public function getProdiPilihan2()
{
    return $this->hasOne(ProdiPenerima::className(), ['id_prodi_penerima' => 'prodi_pilihan_2']);
}

And this is ProdiPenerima model:

...
* @property Pendaftar[] $pendaftars
* @property Pendaftar[] $pendaftars0
...
/**
 * @return \yii\db\ActiveQuery
 */
public function getPendaftars()
{
    return $this->hasMany(Pendaftar::className(), ['prodi_pilihan_1' => 'id_prodi_penerima']);
}

/**
 * @return \yii\db\ActiveQuery
 */
public function getPendaftars0()
{
    return $this->hasMany(Pendaftar::className(), ['prodi_pilihan_2' => 'id_prodi_penerima']);
}

prodi_pilihan_1 and prody_pilihan_2 are foreign keys in pendaftar table, that key was referenced from ProdiPenerima table.


回答1:


$result = ProdiPenerima::find()
    ->select([
        '*',
        'jum1' => Pendaftar::find()
            ->select(['COUNT(*)'])
            ->where('pendaftar.prodi_pilihan_1 = a.id_prodi_penerima'),
        'jum2' => Pendaftar::find()
            ->select(['COUNT(*)'])
            ->where('pendaftar.prodi_pilihan_2 = a.id_prodi_penerima')
    ])
    ->alias('a')
    ->asArray()
    ->all();

Results can be accessed by:

foreach ($result as $row) { 
    echo $row['jum1']; 
}

That because asArray() was used, so query return array of arrays instead of array of models.


If you need models, you should add properties into your models to store result of subqueries:

class ProdiPenerima extends ActiveRecord {

    public $jum1;
    public $jum2;

    // ...
}

Then remove isArray() from query:

$result = ProdiPenerima::find()
    ->select([
        '*',
        'jum1' => Pendaftar::find()
            ->select(['COUNT(*)'])
            ->where('pendaftar.prodi_pilihan_1 = a.id_prodi_penerima'),
        'jum2' => Pendaftar::find()
            ->select(['COUNT(*)'])
            ->where('pendaftar.prodi_pilihan_2 = a.id_prodi_penerima')
    ])
    ->alias('a')
    // ->asArray()
    ->all();

Results can be accessed by:

foreach ($result as $model) { 
    echo $model->jum1; 
}

But note that using asArray() will be faster, so unless you need access some model methods (or rely on typecasting of values from DB) I would prefer arrays.



来源:https://stackoverflow.com/questions/50581521/subquery-in-select-using-yii2-activerecord

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!