K
K
Kop3t32015-11-27 16:27:31
MySQL
Kop3t3, 2015-11-27 16:27:31

Yii2 how to make a hole in model id?

Good day!
I am translating the application to yii2. The database has a "pipes" table that contains a list of products sorted by record id (for example:
1 | Thin hose
2 | Medium diameter hose
3 | Thick hose
)
Sometimes you need to add a few more records, bringing it to this form:
1 | Thin hose
2 | Thin reinforced hose
3 | Medium diameter hose
4 | Medium diameter hose, water/gas
5 | Thick hose
In the original, I made a sql query that "punched" a hole in the id list:
UPDATE TABLE `pipes` SET id=id+1 WHERE id > 2;
Is it possible to do the same in yii2 through models?
For example, this option:

$models = Pipes::find()->where("id > 2")->all();
foreach ($models as $model){
$model->id++;
$model->save();
}

However, this generates a whole bunch of queries. Is there a more versatile option?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sunrails, 2015-11-27
@Kop3t3

Add a sort_order field to the table and rearrange the sort to your heart's content. And making a "hole" is a really bad idea.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question