Doctrine 2 - ORM
  1. Doctrine 2 - ORM
  2. DDC-3120

Warning: Erroneous data format for unserializing PHP5.6+

    Details

    • Type: Bug Bug
    • Status: Resolved
    • Priority: Critical Critical
    • Resolution: Fixed
    • Affects Version/s: Git Master
    • Fix Version/s: 2.5
    • Component/s: ORM
    • Security Level: All
    • Labels:
    • Environment:
      Webserver Apache/2.4.7 (Win32) OpenSSL/1.0.1e PHP/5.6.0beta2

      and

      PHP-CLI (Win32) PHP/5.6.0beta2

      Description

      Hi all,

      There seems to be something strange going on in the method newInstance() of the class \Doctrine\ORM\Mapping\ClassMetadataInfo.

      The original class method looks like this:

      \Doctrine\ORM\Mapping\ClassMetadataInfo#newInstance()

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

      What happens now when a class that implements \Serializable is that a "Warning: Erroneous data format for unserializing" shows up and the function unserialize() returns false.

      That is because a class that implements \Serializable is expected to have the letter 'C' in the serialize string instead of the letter 'O'.

      I've made a quick work-around like this:

      \Doctrine\ORM\Mapping\ClassMetadataInfo#newInstance()

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

      That seems to work in my isolated tests and with Symfony2 and Doctrine2 and FOSUserBundle together.

      I've noticed this because the Model\User class from FOSUserBundle implements \Serializable.

      I had to implement a check in Model\User class because when using 'C:%d:"%s":0:{}' the $serialized parameter of the unserialize method in the Model\User class is a empty string then.

      That warning seems only to happen with PHP5.6+. PHP5.5.12 and below doesn't show that warning.

      I hope someone can shine some light on this, thank you,

      Cornelis.

        Issue Links

          Activity

          Hide
          Doctrine Bot added a comment -

          A related Github Pull-Request [GH-1109] was closed:
          https://github.com/doctrine/doctrine2/pull/1109

          Show
          Doctrine Bot added a comment - A related Github Pull-Request [GH-1109] was closed: https://github.com/doctrine/doctrine2/pull/1109
          Hide
          Marco Pivetta added a comment -

          Due to a new introduced dependency, this issue will only be solved for PHP 5.6 in 2.5.0 and later.

          Show
          Marco Pivetta added a comment - Due to a new introduced dependency, this issue will only be solved for PHP 5.6 in 2.5.0 and later.
          Hide
          Doctrine Bot added a comment -

          A related Github Pull-Request [GH-1109] was assigned:
          https://github.com/doctrine/doctrine2/pull/1109

          Show
          Doctrine Bot added a comment - A related Github Pull-Request [GH-1109] was assigned: https://github.com/doctrine/doctrine2/pull/1109
          Hide
          Dominik Zogg added a comment -

          @ocramius would be great if there where backports for 2.4 and 2.3

          Show
          Dominik Zogg added a comment - @ocramius would be great if there where backports for 2.4 and 2.3
          Hide
          Attila Bukor added a comment -

          I agree with Dominik, we'd like to use the latest stable version and would need this patch.

          Show
          Attila Bukor added a comment - I agree with Dominik, we'd like to use the latest stable version and would need this patch.

            People

            • Assignee:
              Marco Pivetta
              Reporter:
              Cornelis Brouwers
            • Votes:
              0 Vote for this issue
              Watchers:
              13 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: