简体   繁体   English

Yii2,ActiveQuery搜索模型(如果存在关系模型)

[英]Yii2, ActiveQuery search model if there are existing relation models

There are 2 Active Record models Questions( Q ) and Answers( A ). Active Record模型有2个问题( Q )和答案( A )。

For simplicity I omit unsignificant attributes: 为简单起见,我忽略了不重要的属性:

  • Q : question_id :question_id
  • A : answer_id, question_id :answer_id,question_id

Q hasMany A , link by question_id Q hasMany A ,通过question_id链接

public function getAnswers()
{
    return $this->hasMany(Answer::className(), ['question_id' => 'question_id']);
}

How can I create find query for finding only Q models, which have at least one related A model? 如何创建只查找至少具有一个相关A模型的Q模型的查找查询?

This query require in ActiveDataProvider in basic Q Search model. 此查询要求在基本Q搜索模型的ActiveDataProvider中进行。

使用innerJoinWithdoc ):

Q::find()->innerJoinWith(['answers'])->all();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM