skip to Main Content

I have done data migration using data migration tool from magento 1.9.x to 2.2.4, but it doesn’t import admin users as mentioned in the docs, and we need to manually copy the admin users.

What I have done is, I have simply copied the users from magento1DB.admin_user to magento2DB.admin_user table. I can see that users are now appearing in the Magento2 backend, but when I try to edit any admin user, it throws an exception.

Exception #0 (InvalidArgumentException): Unable to unserialize value

Also, I cannot login with the Magento1 admin user in Magento2 admin panel.

Couldn’t find any help, does any one have an idea?

2

Answers


  1. The problem is in /vendor/magento/framework/Serialize/Serializer/Json.php there is a
    function unserialize($string)

    There is a workaround – you can check if string is serialized and then use serialize($string).
    Change unserialize to:

    public function unserialize($string)
    {
        /* Workaround: serialize first if is serialized */
        if($this->is_serialized($string))
        {
            $string = $this->serialize($string);
        }
        $result = json_decode($string, true);
        if (json_last_error() !== JSON_ERROR_NONE) {
             throw new InvalidArgumentException('Unable to unserialize value.');
    
        }
        return $result;
    }
    

    and add function to check if string is serialized:

    function is_serialized($value, &$result = null)
    {
        // Bit of a give away this one
        if (!is_string($value))
        {
            return false;
        }
        // Serialized false, return true. unserialize() returns false on an
        // invalid string or it could return false if the string is serialized
        // false, eliminate that possibility.
        if ($value === 'b:0;')
        {
            $result = false;
            return true;
        }
        $length = strlen($value);
        $end    = '';
        switch ($value[0])
        {
            case 's':
                if ($value[$length - 2] !== '"')
                {
                    return false;
                }
            case 'b':
            case 'i':
            case 'd':
                // This looks odd but it is quicker than isset()ing
                $end .= ';';
            case 'a':
            case 'O':
                $end .= '}';
                if ($value[1] !== ':')
                {
                    return false;
                }
                switch ($value[2])
                {
                    case 0:
                    case 1:
                    case 2:
                    case 3:
                    case 4:
                    case 5:
                    case 6:
                    case 7:
                    case 8:
                    case 9:
                        break;
                    default:
                        return false;
                }
            case 'N':
                $end .= ';';
                if ($value[$length - 1] !== $end[0])
                {
                    return false;
                }
                break;
            default:
                return false;
        }
        if (($result = @unserialize($value)) === false)
        {
            $result = null;
            return false;
        }
        return true;
    }
    
    Login or Signup to reply.
  2. Changing in core file is not good idea, so I would recommend to just identify the table by reviewing exception then describe that table to see which field can consist searialized value, find it by select query, once you identify the serialized field in table the convert that to json and update to table, Here is code to convert serialized data to json data:

    // Pull serialized data 
    $serializeddata = 'a:2:{i:6517;a:2:{i:0;a:5:{s:10:"first_name";s:5:"Roger";s:9:"last_name";s:6:"Rabbit";s:5:"email";s:19:"[email protected]";s:7:"is_lead";b:1;s:12:"is_cancelled";b:0;}i:1;a:5:{s:10:"first_name";s:7:"Jessica";s:9:"last_name";s:6:"Rabbit";s:5:"email";s:21:"[email protected]";s:7:"is_lead";b:0;s:12:"is_cancelled";b:0;}}i:6518;a:2:{i:0;a:5:{s:10:"first_name";s:6:"Mickey";s:9:"last_name";s:5:"Mouse";s:5:"email";s:20:"[email protected]";s:7:"is_lead";b:0;s:12:"is_cancelled";b:0;}i:1;a:5:{s:10:"first_name";s:6:"Donald";s:9:"last_name";s:4:"Duck";s:5:"email";s:20:"[email protected]";s:7:"is_lead";b:0;s:12:"is_cancelled";b:0;}}}';
    
    // Unserialize it into a standard array
    $array = unserialize($serializeddata);
    
    $jsonData = json_encode($array);
    
    // Print Array
    echo $jsonData;
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search