28

I'm back with another issue regarding my UserBundle : Everything went perfect while installing and configuring FOS bundle through Symfony2, it even let me create 2 users that were properly inserted into my DB.

However, every time I try to login into either of these accounts, I get the following error

Warning: Erroneous data format for unserializing 'VillaPrivee\UserBundle\Entity\User' in /Users/Vianney/Projets/VillaPrivee/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php line 869

This is what the line 869 refers to :

/**
     * Creates a new instance of the mapped class, without invoking the constructor.
     *
     * @return object
     */
    public function newInstance()
    {
        if ($this->_prototype === null) {
            $this->_prototype = unserialize(sprintf('O:%d:"%s":0:{}', strlen($this->name), $this->name));
        }

        return clone $this->_prototype;
    }

And this is my User entity :

namespace VillaPrivee\UserBundle\Entity;

use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="fos_user")
 */
class User extends BaseUser
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    public function __construct()
    {
        parent::__construct();
        // your own logic
    }
}

Not sure what I did wrong, since I just installed the whole thing following the step by step documentation... Thanks guys for your help

wattever
  • 611
  • 1
  • 7
  • 12

5 Answers5

39

If you are using PHP Version 5.4.29 or 5.5.13

In: "/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php" find function "newInstance" (around Line 827) and edit as followed until the Fix is merged by doctrine.

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;
}

@Benji: thx for the hint: https://github.com/symfony/symfony/issues/11056

grow
  • 687
  • 5
  • 9
  • 1
    Or just upgrade your PHP version to fix this. I upgraded fra 5.4.29 to 5.4.30, which made everything work as expected :) – Jeppe Mariager-Lam Aug 24 '14 at 06:12
  • 2
    This is an issue with the PHP 5.6 branch as well. As you can see this has been addressed in [Doctrine 2.4.6](https://github.com/doctrine/doctrine2/blob/bebacf79d8d4dae9168f0f9bc6811e6c2cb6a4d9/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php#L866). – jcroll Oct 14 '14 at 21:36
  • 2
    Yep. Confirming problem on 5.6 :) Debian unstable - 5.6.2+dfsg-1 version. Had to add PHP_VERSION_ID === 50602 for my version. – Anton Valqk Dec 15 '14 at 13:21
  • Nice !! `PHP 5.6.7-1 (cli) (built: Mar 24 2015 12:30:15)` here I tried 1st with this suggestion http://www.doctrine-project.org/jira/browse/DDC-3120 with no luck. changing O by C yields empty String and crashes in User::unserialize() which expects array. – juanmf May 03 '15 at 17:25
  • `PHP_VERSION_ID === 50609` for PHP 5.6.9-0+deb8u1 – Laurent W. Jul 20 '15 at 15:33
  • `50613 ` for me. What a hacky function. Who's to blame for this nonsense? :D – keyboardSmasher Sep 21 '15 at 16:15
  • If not possible to update doctrine to 2.5, better to use `if (method_exists($this->reflClass, 'newInstanceWithoutConstructor'))` instead of versions comparsion – Stafox Dec 10 '15 at 14:44
  • If you read that today, use `if (PHP_VERSION_ID > 50513)` as condition so you don't have to add every intermediate PHP version that you may use. (radical but effective) – Yoan Tournade Jan 24 '18 at 23:12
18

Answering my own question, I found a workaround thanks to this guy :

http://www.doctrine-project.org/jira/browse/DDC-3120

He's far better than me when it comes to explaining, but this is what I have now, and it works like a charm! :)

{
        if ($this->_prototype === null) {
            $this->_prototype = @unserialize(sprintf('O:%d:"%s":0:{}', strlen($this->name), $this->name));
            if ($this->_prototype === false) {
                $this->_prototype = @unserialize(sprintf('C:%d:"%s":0:{}', strlen($this->name), $this->name));
            }
        }

        return clone $this->_prototype;
    }
wattever
  • 611
  • 1
  • 7
  • 12
  • I also experienced the same error with `PHP 5.6.3`, but I don't think this one would solve the problem. We know that in PHP when an @ sign is prefixed to an expression, any error messages that might be generated by that expression will be ignored. See http://php.net/manual/en/language.operators.errorcontrol.php – user123 May 06 '15 at 23:56
3
  1. Check your PHP version by "php -v" on command line. E.g. PHP 5.6.10

  2. Edit the file /vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php::newInstance()

Add your PHP_VERSION_ID here

if (PHP_VERSION_ID === 50610 ) {
.
}

It's a temporary solution, since we don't edit vendor directory.

R Sun
  • 1,353
  • 14
  • 17
1

I experienced the same problem with php 5.6.6 (locally with mamp), my workaround was just to go back to 5.4, since that's the version I had running on the Server anyways... but definately something to keep in mind...

AlexK
  • 471
  • 1
  • 5
  • 14
0

This code works for me, YourApplicationName\vendor\doctrine\lib\Doctrine\ORM\Mapping\ClassMetadataInfo.php

i replace the existing code with the below code

If ($this->_prototype === null) {
    $this->_prototype = @unserialize(sprintf('O:%d:"%s":0:{}', strlen($this->name), $this->name));
if ($this->_prototype === false) {
    $this->_prototype = unserialize(sprintf('C:%d:"%s":0:{}', strlen($this->name), $this->name));
}
}
Snopzer
  • 1,602
  • 19
  • 31