A
A
Alexander Stepanov2018-02-17 01:11:57
Yii
Alexander Stepanov, 2018-02-17 01:11:57

How to select records starting with a number in yii2?

You need to select records by the first character of a letter or number.
In the case of letters, everything is simple. is selected one by one by the expression:

Model::find()->where(['like', 'name', $letter . '%', false, 'status' => 1])->andWhere(['status' => 1])->all();

But with numbers, you need to select not for each digit, but for the digit in general [0-9].
The numbers in the $numbers array. Substituted $numbers, but swears that the array
Tried to replace $letter with regular expressions, but something is not right.
How can I select all records whose first character is a digit.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
OKyJIucT, 2018-02-17
@OKyJIucT

Make a separate field for the letter index, and when creating, save the first character of the name in the index with the model. And then choose according to it, and you will not need to use LIKE queries, which are not the fastest, especially if you need to display all records from the database that begin with the desired letter.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question