2017-09-20 58 views
0

我已经在yii2以下型号:子查询的ActiveRecord

use frontend\modules\bewerber\models\Bewerber; 
use common\modules\basis\models\base\Person; 
use common\modules\lookup\models\LAnrede; 

如何创建使用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); 

PS:最后的WHERE子句应不固定,但变量是这样的:

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

将推出以下结果:先生小姐

... .................................................. ...........

提示Fabrizio Caldarelli

............................................ ....................

乌尔解决方案不会帮助我:=(

这是我们的代码:

$idPerson = 1; 
 

 
$show=LAnrede::find()->joinWith(['Person' => function($q) use($idPerson) { 
 
     $q->andWhere([ 
 
      'Person.id' => (new \yii\db\Query())->from('Bewerber')->where(['Bewerber.id_person' => $idPerson]) 
 
     ])->anrede; 
 
}]);

,这是var_dump($show);

E:\xampp\htdocs\yii2_perswitch\frontend\modules\bewerber\views\bewerber\index.php:48: 
 
object(common\modules\lookup\models\LAnredeQuery)[207] 
 
    public 'sql' => null 
 
    public 'on' => null 
 
    public 'joinWith' => 
 
    array (size=1) 
 
     0 => 
 
     array (size=3) 
 
      0 => 
 
      array (size=1) 
 
       ... 
 
      1 => boolean true 
 
      2 => string 'LEFT JOIN' (length=9) 
 
    public 'select' => null 
 
    public 'selectOption' => null 
 
    public 'distinct' => null 
 
    public 'from' => null 
 
    public 'groupBy' => null 
 
    public 'join' => null 
 
    public 'having' => null 
 
    public 'union' => null 
 
    public 'params' => 
 
    array (size=0) 
 
     empty 
 
    private '_events' (yii\base\Component) => 
 
    array (size=0) 
 
     empty 
 
    private '_behaviors' (yii\base\Component) => 
 
    array (size=0) 
 
     empty 
 
    public 'where' => null 
 
    public 'limit' => null 
 
    public 'offset' => null 
 
    public 'orderBy' => null 
 
    public 'indexBy' => null 
 
    public 'emulateExecution' => boolean false 
 
    public 'modelClass' => string 'common\modules\lookup\models\LAnrede' (length=36) 
 
    public 'with' => null 
 
    public 'asArray' => null 
 
    public 'multiple' => null 
 
    public 'primaryModel' => null 
 
    public 'link' => null 
 
    public 'via' => null 
 
    public 'inverseOf' => null

我使用的GridView这样

$gridColumn = [ 
 
    [ 
 
     'attribute' => '', 
 
     'label' => Yii::t('app', 'Anrede'), 
 
     'format' => 'html', 
 
     'value' => function($model) { 
 
      return "<p><font color='green'>" . LAnrede::findOne([$model->id_person])->anrede . "</p>"; 
 
     } 
 
    ], 
 
    ];

Colud u显示了我如何用UR解决方案在这方面?

回答

1

这应该工作:

$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]) 
     ]); 
}]) 
->all(); 

'人' 是LAnrede模型的关系

public function getPerson() 
{ 
    return $this->hasMany(Person::className(), ['id_anrede' => 'id']); 
} 
+0

的关系已经由GII – tklustig

+0

OK设定,所以采取(一个或多个关系?)只有第一个片段。 –

+0

请再读一遍我的帖子,我改了! – tklustig