R
R
Ruslan Kasymov2016-04-20 15:09:05
MySQL
Ruslan Kasymov, 2016-04-20 15:09:05

How to organize your sort order in YII2?

How to organize your sort order in YII2?
For example, there is a model Clients table clients (mysql) you need to create your own order.
It comes to mind how to add, say, the sort field to the table and swap the values ​​\u200b\u200bin there. Can it be solved in some other way?
If not, how can I add values ​​to this field? MySQL does not allow autoincrement for more than 1 field, it turns out either to write a trigger, or through YII in the model in the afterSave method to write update it will turn out 2 queries after insert immediately update
How to solve this problem correctly?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
justpusher, 2016-05-01
@justpusher

Usually they use a separate field, order or position, and fill it separately. If only by means of ORM - then only beforeSave / afterSave (I like the first one more). If you can write your own save query, you can solve it with a single INSERT query with a subquery.
If you really want to use ORM tools and one request, you can insert the current date-time with microseconds into the order field. But this is a perversion, I think. There is nothing wrong with two queries.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question