Answer the question
In order to leave comments, you need to log in
How to write this sql code as ActiveRecord?
SELECT param.id
FROM param
WHERE NOT EXISTS (
SELECT userparam.*
FROM userparam
WHERE userparam.param_id = param.id AND userparam.user_id = 42
)
AND EXISTS (
SELECT catparam.*
FROM catparam
WHERE catparam.parameter_id = param.id AND catparam.category_id IN (1, 2)
)
Answer the question
In order to leave comments, you need to log in
Доброе утро.
Приблизительно так:
$query1 = new \yii\db\Query();
$query1->select('userparam.*')
->from('userparam')
->where(['userparam.id' => 'param.id'])
->andWhere(['userparam.user_id' => 42])->one();
$query2 = new \yii\db\Query();
$query2->select('catparam.*')
->from('catparam')
->where(['catparam.parameter_id' => 'param.id'])
->andWhere(['in', 'catparam.category_id', [1,2]])->all();
$params = Param::find()
->select('id')
->where(['not exist' => $query1])->andWhere(['exists' => $query2])->all();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question