A
A
Alexander Verbitsky2018-09-10 02:44:05
Yii
Alexander Verbitsky, 2018-09-10 02:44:05

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

1 answer(s)
Дмитрий, 2018-09-10
@slo_nik Куратор тега Yii

Доброе утро.
Приблизительно так:

$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();

Подробней тут и тут.
p.s. Будут в запросе ошибки, проверить не могу.)))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question