E
E
EvgMul2017-04-17 14:35:13
Yii
EvgMul, 2017-04-17 14:35:13

How to make a between query in Yii2?

Hello. It is not possible to correctly compose a between query using Yii2.
At the moment I have this option:

$ips = Ip::find()->where("'$userIp' between ip_start and ip_end")->one();

But I don't like it because where is passed a string.
Google gave me this option:
$model = ModelName::find()->where(['between', 'date', "2015-06-21", "2015-06-27" ])->all();

But this query does a slightly different operation.
I need the query to find a record that satisfies the condition so that my value is within the boundaries of 2 fields.
The second query finds records whose boundaries are specified for one field.
Can you please tell me if it is possible, and if so, how can I make a request using Yii2?
Well, or at least make the current request more secure?
Thanks in advance to all who respond.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
davidnum95, 2017-04-17
@EvgMul

$ips = Ip::find()
                ->where(['>', 'ip_start', $userIp])
                ->andWhere(['<', 'ip_end', $userIp])
                ->one();

M
Maxim Timofeev, 2017-04-17
@webinar

Quoting the docs :
between: operand 1 should be the column name, and operand 2 and 3 should be the starting and ending values ​​of the range that the column is in. For example, ['between', 'id', 1, 10] will generate id BETWEEN 1 AND 10
https://github.com/yiisoft/yii2/blob/master/docs/g...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question