SubQuery in ActiveRecord

Hi guys,

I have following models in yii2:




use frontend\modules\bewerber\models\Bewerber;

use common\modules\basis\models\base\Person;

use common\modules\lookup\models\LAnrede;



How to create following query using methods of ActiveRecord?




SELECT anrede FROM L_anrede JOIN Person ON L_anrede.id=Person.id_anrede WHERE Person.id IN

(SELECT id_person FROM Bewerber WHERE Bewerber.id_person=1);



P.S.: The last WHERE clause should be not fix but variable like this:


var_dump(LAnrede::findOne([$model->id_person])->anrede)



which will output:Misteror Miss

This should work:




$idPerson = 1;


LAnrede::find()->joinWith(['Person' => function($q) use($idPerson) {

       $q->andWhere([

             'Person.id' => (new \yii\db\Query())->from('Bewerber')->where(['Bewerber.id_person' => $idPerson])

       ]);

}]);



‘Person’ is a relation in LAnrede model (one or many relation?)




public function getPerson()

{

    return $this->hasMany(Person::className(), ['id_anrede' => 'id']);

} 



Colud u show me up how to use ur solution in this context,please?Thx a lot for ur help since now… ::)

What field do you want to show in grid column?

anrede from l_anrede.

l_anrede has two attributes:PK(id) && anrede(VARCHAR)