K
K
kotepdag2020-01-27 15:44:00
Yii
kotepdag, 2020-01-27 15:44:00

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

but in order to load into the categoryValue connection

I do this:
Category::find()
            ->innerJoin(['categoryValue' => CategoryValueQuery::get($id, $language_id)], 'categoryValue.category_id = category.id')
            ->andWhere(['category.id' => $id])
            ->one();

categoryValue relationship is empty.

I do it like this:
Category::find()
->joinWith(['categoryValue' => function($query) use ($id, $language_id) {
    return CategoryValueQuery::get($id, $language_id)->all();
// $query = ^ тоже самое
 }])
->andWhere(['category.id' => $id])
->one();

So generally nested request does not participate in anything.

Here is the categoryValue relationship in the Category model

public function getCategoryValue()
    {
        return $this->hasOne(CategoryValue::className(), ['category_id' => 'id']);
    }


Can you please tell me how to make a connection using a nested query?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question