I
I
Iossarian2019-07-22 17:01:48
Yii
Iossarian, 2019-07-22 17:01:48

How to rewrite sql query in ActiveQuery?

Good afternoon. There is a not quite simple sql query that needs to be rewritten in ActiveQuery in order to avoid possible sql injections. The actual request:

SELECT 
`policy`.`id`,
`operator_id` as 'id оператора',
`email оператора`,
`ФИО`,
`av_user`.`subdivision` as 'Подразделение от АВ',
`av_user`.`curator` as 'Куратор от АВ',
`policy`.`payed_date` as 'Дата оплаты',
`policy`.`cost` as 'СП',
`policy`.`user_remuneration` as 'КВ'

FROM  (select *,
replace(JSON_EXTRACT(`parameters`, '$.users.operator.email'), '"', '') as `email оператора`,
replace(JSON_EXTRACT(`parameters`, '$.users.operator.full_name'), '"', '') as `ФИО`,
JSON_EXTRACT(`parameters`, '$.users.operator.id') as `operator_id`
FROM `partner__data` 
) as tt 
LEFT JOIN `av_user` on `av_user`.`id` = tt.`operator_id`
LEFT JOIN `policy` on `policy`.`id` = tt.`policy_id`
WHERE LOWER(`ФИО`) like LOWER('%$name%')
AND policy.`payed_date` between '$min' and '$max' 
GROUP BY policy.`payed_date` 
ORDER BY policy.`payed_date`

Everything would be fine if not
FROM  (select *,
replace(JSON_EXTRACT(`parameters`, '$.users.operator.email'), '"', '') as `email оператора`,
replace(JSON_EXTRACT(`parameters`, '$.users.operator.full_name'), '"', '') as `ФИО`,
JSON_EXTRACT(`parameters`, '$.users.operator.id') as `operator_id`
FROM `partner__data` 
) as tt

Such a moment is misleading. Please tell me how to do it right? Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim, 2019-09-16
@Iossarian

Why not use Expression ?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question