[英]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 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中进行。
使用innerJoinWith
( doc ):
Q::find()->innerJoinWith(['answers'])->all();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.