yii2: Active Record Optimistic locking does not work with multiple models

What steps will reproduce the problem?

Controller code:

if (Model::loadMultiple($models, Yii::$app->request->post()) && Model::validateMultiple($models)) {
            $count = 0;
            foreach ($models as $index => $modelIN) {
                if ($modelIN->save()) {
                    $count++;
                }
            }
        }

Controller gets POST input:

| Array – | –   | (   | [_csrf] => plOgdUA529aMw6mz-uan96qT-1Ty2NS2KbTasCtqK-LVHPpHK363–aB7di_it-23uuDLsP1neBuwIyCSC942g==   | [ShopItem] => Array   | (   | [lock_version] => 1   | [product_type] => group   | [frontend_name] => Main product name   | [sku] => 3904-G   | [available_yn] => 1   | …   | [55] => Array   | (   | [id] => 55   | [lock_version] => 3   | [frontend_name] => Included product name   | [sku] => 9838   | [in_stock_yn] => 1   | [stock_available] => 1   | [price] => 98.00   | [old_price] => 106.00   | )   |     | )   | ) – | –   |

What is the expected result?

All items should be updated correctly given they are not outdated

What do you get instead?

yii\db\StaleObjectException: The object being updated is outdated. in

Copy Stacktrace Search Stackoverflow Search Google Exception Stale Object Exception – yii\db\StaleObjectException The object being updated is outdated.

Additional info

Q A
Yii version 2.0.15.1
PHP version 7.2.10
Operating system Win 7 (x64)

About this issue

  • Original URL
  • State: open
  • Created 6 years ago
  • Comments: 18 (1 by maintainers)

Most upvoted comments

I hope you can solve your problem, your comment about the comedy made me laught because it felt relieve about another developer not using laravel. I never had the issue you are having but i had others and always managed to solve then, honestly everything i had worked brings challenges, from PHP to C# to Android there is always stuff like this, for me the most important is to have a well implemented framework that’s why i’m a Yii fanatic…yes i admit this…i’m a fanatic…

i’m happy you wont use Laravel 😉

El lun., 8 abr. 2019 a las 10:15, strtob (notifications@github.com) escribió:

…thanks to share your experience with laravel. In general I’m happy with yii2, but when the app will throw the error furthermore, a normal use can’t handle this 😦 I will try the $this->updateCounters(…) and hope this will fix the problem

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/yiisoft/yii2/issues/16889#issuecomment-480874935, or mute the thread https://github.com/notifications/unsubscribe-auth/AcijSNQayckY7dxe8c6wM4C5W1DEhOWcks5ve10IgaJpZM4YePGY .