S
S
slip312019-04-25 22:11:19
Yii
slip31, 2019-04-25 22:11:19

How to change order order?

You need to change the order of the menu items. They are sorted by the 'order' field, which has the unique status in the database.
It is necessary to make the functionality of changing the places of menu items - the item "contact" was in second place, became in first. Decided on the forehead first

public static function changeOrder($cur, $new) {
        if ((isset($cur)) && (isset($new)) && ($cur !== $new)) {
            $currentModel = parent::findOne($cur);
            $newModel = parent::findOne($new);                 
            $currentModel->order = $new;
            $newModel->order = $cur;
            if ($currentModel->save() && $newModel->save()) {
                echo 'works';
            } else {
                var_dump($currentModel->errors);
                var_dump($newModel->errors);
            }
        }
    }

But, of course, it didn't work, since such an "order" already exists when writing, and the validation fails. How to solve such things? Through a temporary variable?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question