Warning: Erroneous data format for unserializing PHP 5.4.29 and 5.5.13 #11056

Closed
growBremen opened this Issue Jun 4, 2014 · 22 comments

Comments

Projects
None yet

When using PHP Version 5.4.29 or 5.5.13 there is an Warning, which is followed by an Fatal error due to the Object expected is false.

Original Function "newInstance" in \vendor\doctrine\orm\lib\Doctrine\ORM\Mapping\ClassMetadataInfo looks like this:

public function newInstance()
{
    if ($this->_prototype === null) {
        $this->_prototype = unserialize(sprintf('O:%d:"%s":0:{}', strlen($this->name), $this->name));
    }
    return clone $this->_prototype;
}

Quickfix for this issues:

public function newInstance()
{
    if ($this->_prototype === null) {
        // $this->_prototype = unserialize(sprintf('O:%d:"%s":0:{}', strlen($this->name), $this->name));
        if (PHP_VERSION_ID === 50429 || PHP_VERSION_ID === 50513) {
            $this->_prototype = $this->reflClass->newInstanceWithoutConstructor();
        } else {
            $this->_prototype = unserialize(sprintf('O:%d:"%s":0:{}', strlen($this->name), $this->name));
        }
    }
    return clone $this->_prototype;
}

See also:

doctrine/doctrine2#1045
http://www.doctrine-project.org/jira/browse/DDC-3120
http://stackoverflow.com/questions/24031048/fos-userbundle-unable-to-login/24035854#24035854

Member

stof commented Jun 4, 2014

This is a Doctrine issue, not a Symfony issue.

And this is already fixed in the dev version of Doctrine

@stof stof closed this Jun 4, 2014

oopen commented Jun 23, 2014

Same error with PHP Version 5.6.0beta4

To fix it add : || PHP_VERSION_ID === 50600

public function newInstance()
{
    if ($this->_prototype === null) {
         if (PHP_VERSION_ID === 50429 || PHP_VERSION_ID === 50513 ||  PHP_VERSION_ID === 50600) {
            $this->_prototype = $this->reflClass->newInstanceWithoutConstructor();
        } else {
            $this->_prototype = unserialize(sprintf('O:%d:"%s":0:{}', strlen($this->name), $this->name));
        }
    }
     return clone $this->_prototype;
}

phansys referenced this issue in doctrine/doctrine2 Jul 16, 2014

I have the same error with Symfony 2.3 on PHP V5.6

Make sure you use correct PHP_VERSION_ID value :)
In my case, it was PHP_VERSION_ID = 50607 for PHP 5.6.7

nask0 commented May 14, 2015

Still exists on doctrine 2.5.0+ and PHP5.6+
https://travis-ci.org/allan-simon/oauth2-symfony2-vagrant-fosuserbundle/jobs/62429219
(This is not my bug report i just saw it few days ago on the internet)

@nask0 funny, i was looking for the error message to see if i could do something about it , and google lend me to that page ...

timonf commented Jun 11, 2015

I had the same error message on Symfony 2.3 and 2.7 using Doctrine ORM 2.3. I'm using PHP 5.6.4-4ubuntu6. I solved this problem by upgrading Doctrine ORM to 2.4.*

Contributor

sstok commented Aug 4, 2015

@anteriovieira Fala inglês por favor (Speak English please) ;)

Hello @sstok , sorry.
The suggestion of @timonf is valid. Updated version of the doctrine to [2.4. *] and it worked fine.

@anteriovieira : do you mean that 2.5 does not work, but sticking on 2.4.* works ?

Good morning my friend @allan-simon, in version 2.5 work. I'm using my composer as follows:

    //...
    "doctrine/orm": "~2.2,>=2.2.3,<2.5",
    //...

@anteriovieira , ok i'm gonna try that, thanks.

@anteriovieira 's fix works perfect for me! Obrigado! ;-)

valVk commented Dec 25, 2015

@anteriovieira
Thanks 👍

I've upgraded the composer.lock of my project recently and now it works.

Another thanks to @anteriovieira, that version setting saved my bacon.

Thanks for the acknowledgments, but all credit is for @timonf .

I had the same error message on Symfony 2.3 and 2.7 using Doctrine ORM 2.3. I'm using PHP 5.6.4-4ubuntu6. I solved this problem by upgrading Doctrine ORM to 2.4.*

nask0 commented Jan 18, 2016

The "PHP_VERSION_ID" fix is just ridiculous as some of symfony developers (sorry, no mean to offend anybody).

Thank, i updated doctrine, in composer.json "doctrine/orm": "~2.4" and after composer update

diriy commented Aug 3, 2016

who's more new? I use Symfony 3.1 and get this error. Windows 7 64x, PHP 5.6. How can I solve it?

8khan commented Nov 22, 2017

i khow this it an old topic, but i solve it with this
you need khow your php version, can add a var_dump(PHP_VERSION_ID); on app.php
then add your version on line 830 on \www\vendor\doctrine\orm\lib\Doctrine\ORM\Mapping\ClassMetadataInfo.php file
another think that help is able display php errors

The patch proposed @8khan and @oopen still work fine.

If you directly want to patch against any PHP version greated than PHP 5.4, you can (more radically) do:

public function newInstance()
{
    if ($this->_prototype === null) {
         if (PHP_VERSION_ID > 50429) {
            $this->_prototype = $this->reflClass->newInstanceWithoutConstructor();
        } else {
            $this->_prototype = unserialize(sprintf('O:%d:"%s":0:{}', strlen($this->name), $this->name));
        }
    }
     return clone $this->_prototype;
}

(Important part is PHP_VERSION_ID > 50429)

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