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

AuditableTransitionException #432

Closed
xyrintech opened this Issue Jun 20, 2018 · 6 comments

Comments

Projects
None yet
3 participants
@xyrintech

xyrintech commented Jun 20, 2018

Q A
Bug? yes
New Feature? no
Framework Laravel
Framework version 5.5
Package version 7.0
PHP version 7.0

Actual Behaviour

Error on transitionTo()
{"message":"Expected Auditable id 55, got 55 instead","status_code":500}

It works completely fine on server but on production server. Both are ubuntu servers.

@quetzyg

This comment has been minimized.

Member

quetzyg commented Jun 20, 2018

If you don't provide more information, I can't help you, so...

@xyrintech

This comment has been minimized.

xyrintech commented Jun 20, 2018

Please suggest what all information shall I share?
My concern is that if I am using transitionTo() on one server (say server A) it works, when I deploy it another server (say sever B) then this does not work.

This gives an error {"message":"Expected Auditable id 55, got 55 instead","status_code":500}

Here 55 is the ID of the record which I want to restore.

@quetzyg

This comment has been minimized.

Member

quetzyg commented Jun 20, 2018

Please suggest what all information shall I share?

Whatever helps me, help you, don't you think? I have no idea what your code is, tables, models, etc.

@quetzyg quetzyg added the V7 label Jun 20, 2018

@quetzyg quetzyg self-assigned this Jun 20, 2018

@quetzyg

This comment has been minimized.

Member

quetzyg commented Jun 21, 2018

Closing due to lack of information.

@quetzyg quetzyg closed this Jun 21, 2018

@chiiya

This comment has been minimized.

chiiya commented Sep 26, 2018

This occurs when using SQLite as the database (fairly standard for testing), since values are returned as strings, meaning that the comparison:

// The Audit must be for this specific Auditable model
if ($this->getKey() !== $audit->auditable_id) {
    throw new AuditableTransitionException(sprintf(
        'Expected Auditable id %s, got %s instead',
        $this->getKey(),
        $audit->auditable_id
    ));
}

will return true. Laravel automatically casts the id of a model to an integer, but the auditable_id won't be cast, so 1 !== "1".
Not sure what an easy fix for this would be (well, using loose comparison would solve it of course), since not everyone is going to be using auto-incrementing integer primary keys. Overriding the Audit implementation always works:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Audit extends Model implements \OwenIt\Auditing\Contracts\Audit
{
    use \OwenIt\Auditing\Audit;

    /**
     * {@inheritdoc}
     */
    protected $guarded = [];

    /**
     * {@inheritdoc}
     */
    protected $casts = [
        'auditable_id' => 'integer',  // This is important
        'old_values' => 'json',
        'new_values' => 'json',
    ];
}
@quetzyg

This comment has been minimized.

Member

quetzyg commented Sep 26, 2018

Ha! I see, now. Yeah, that's a tricky one. Your suggestion might be the best way to accommodate for this issue.

Thanks a lot @xyrintech, I'll eventually update the documentation to cover this case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment