Answer the question
In order to leave comments, you need to log in
How to create a relationship with a subquery?
Hi all.
There is a Model with a relationship , but for the relationship I need to use a nested query
something like:
SELECT * FROM `category` INNER JOIN <b>(SELECT * FROM `category_value` WHERE ... UNION ...)</b> WHERE category.id = 2
Category::find()
->innerJoin(['categoryValue' => CategoryValueQuery::get($id, $language_id)], 'categoryValue.category_id = category.id')
->andWhere(['category.id' => $id])
->one();
Category::find()
->joinWith(['categoryValue' => function($query) use ($id, $language_id) {
return CategoryValueQuery::get($id, $language_id)->all();
// $query = ^ тоже самое
}])
->andWhere(['category.id' => $id])
->one();
public function getCategoryValue()
{
return $this->hasOne(CategoryValue::className(), ['category_id' => 'id']);
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question