Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The object being updated is outdated. #60

Open
strtob opened this issue Mar 14, 2019 · 5 comments
Open

The object being updated is outdated. #60

strtob opened this issue Mar 14, 2019 · 5 comments

Comments

@strtob
Copy link

strtob commented Mar 14, 2019

Hi,

I've built a crud app with mootensai/yii2-enhanced-gii, there are related tables by foreign keys (n:1, n:m).

In the case I got an validation error, after pressing update a second time the app throw the following error:

`Stale Object Exception – yii\db\StaleObjectException
The object being updated is outdated.

1. in /home/none/work/rebase/vendor/yiisoft/yii2/db/BaseActiveRecord.php at line 813
804805806807808809810811812813814815816817818819820821822

                            if ($lock !== null) {
            $values[$lock] = $this->$lock + 1;
            $condition[$lock] = $this->$lock;
        }
        // We do not check the return value of updateAll() because it's possible
        // that the UPDATE statement doesn't change anything and thus returns 0.
        $rows = static::updateAll($values, $condition);
 
        if ($lock !== null && !$rows) {
            throw new StaleObjectException('The object being updated is outdated.');
        }
 
        if (isset($values[$lock])) {
            $this->$lock = $values[$lock];
        }
 
        $changedAttributes = [];
        foreach ($values as $name => $value) {
            $changedAttributes[$name] = isset($this->_oldAttributes[$name]) ? $this->_oldAttributes[$name] : null;
                

2. in /home/none/work/rebase/vendor/yiisoft/yii2/db/ActiveRecord.php at line 631 – yii\db\BaseActiveRecord::updateInternal(null)
3. in /home/none/work/rebase/vendor/yiisoft/yii2/db/BaseActiveRecord.php at line 681 – yii\db\ActiveRecord::update(true, null)
4. in /home/none/work/rebase/vendor/mootensai/yii2-relation-trait/RelationTrait.php at line 166 – yii\db\BaseActiveRecord::save()
160161162163164165166167168169170171172

                            /* @var $this ActiveRecord */
        $db = $this->getDb();
        $trans = $db->beginTransaction();
        $isNewRecord = $this->isNewRecord;
        $isSoftDelete = isset($this->_rt_softdelete);
        try {
            if ($this->save()) {
                $error = false;
                if (!empty($this->relatedRecords)) {
                    /* @var $records ActiveRecord | ActiveRecord[] */
                    foreach ($this->relatedRecords as $name => $records) {
                        if (in_array($name, $skippedRelations))
                            continue;
                

5. in /home/none/work/rebase/controllers/SalesLeadController.php at line 145 – app\models\base\SalesLead::saveAll()

`

anybody an idea what's the problem?

Cheers!

@danabyrd
Copy link

danabyrd commented Mar 16, 2019

yiisoft/yii2/db/BaseActiveRecord.php
What version of yii are you using? Line 81 of 2.16.1 looks like this:
if ($lock !== null && !$rows) {

Can you try it on 2.16.1 without breaking your project?

Can you think of a test I could do to verify the behavior you want is working?

@strtob
Copy link
Author

strtob commented Mar 18, 2019

Thank you for your help, unfortunately, I still have the problem also with v 2.16.1

`
Stale Object Exception – yii\db\StaleObjectException
The object being updated is outdated.

1. in C:\Users\none\work\rebase\vendor\yiisoft\yii2\db\BaseActiveRecord.php at line 813
804805806807808809810811812813814815816817818819820821822

                            if ($lock !== null) {
            $values[$lock] = $this->$lock + 1;
            $condition[$lock] = $this->$lock;
        }
        // We do not check the return value of updateAll() because it's possible
        // that the UPDATE statement doesn't change anything and thus returns 0.
        $rows = static::updateAll($values, $condition);
 
        if ($lock !== null && !$rows) {
            throw new StaleObjectException('The object being updated is outdated.');
        }
 
        if (isset($values[$lock])) {
            $this->$lock = $values[$lock];
        }
 
        $changedAttributes = [];
        foreach ($values as $name => $value) {
            $changedAttributes[$name] = isset($this->_oldAttributes[$name]) ? $this->_oldAttributes[$name] : null;                  

`

error_log dump:

  • $lock = db_lock (this is my lock column name)
  • $rows = 0

the comment of the BaseActiveRecord is like this:

// We do not check the return value of updateAll() because it's possible // that the UPDATE statement doesn't change anything and thus returns 0. $rows = static::updateAll($values, $condition);

But why should this condition ($lock is the column name && no updates) throws an error?

@strtob
Copy link
Author

strtob commented Mar 21, 2019

Anybody got any idea where is the problem? :-(

@danabyrd
Copy link

@strtob I don't know the cause yet. I've setup a test harness to run an x-debug trace on my database.

Thanks for the information you have sent so far. I will let you know what I find.

@strtob
Copy link
Author

strtob commented Mar 22, 2019

Thank you :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants