[DBAL-73] Could not convert database value "b:0;" to Doctrine Type object Created: 06/Dec/10  Updated: 12/Dec/10  Resolved: 12/Dec/10

Status: Resolved
Project: Doctrine DBAL
Component/s: None
Affects Version/s: 2.0.0-RC1-RC3
Fix Version/s: 2.0-RC5

Type: Bug Priority: Major
Reporter: Marcus Speight Assignee: Benjamin Eberlei
Resolution: Fixed Votes: 0
Labels: None

Attachments: File Setting.php    


The following class when value is set to false causes "Could not convert database value "b:0;" to Doctrine Type object" exception when loading the entity.

/** @orm:Entity */
class MyClass

  • @orm:Column(name="value", type="object")
    public $value;

Comment by Benjamin Eberlei [ 06/Dec/10 ]

From the PHP manual about unserialize():

In case the passed string is not unserializeable, FALSE is returned and E_NOTICE is issued. 

The solution: Use NULL instead of false

Comment by Marcus Speight [ 06/Dec/10 ]

But the string "b:0;" is unserializable it returns false. Also from the php manual on unserialize

FALSE is returned both in the case of an error and if unserializing the serialized FALSE value. 
It is possible to catch this special case by comparing str with serialize(false) or by catching the issued E_NOTICE. 

I've submitted a pull request that handles it correctly.

Comment by Benjamin Eberlei [ 06/Dec/10 ]

I know, but this type is called "object", so setting it to false makes no sense. Setting it to null makes sense though.

I know that b:0; is false, but why do you set it to false anyways? Objects are either null or an instance of the object. Never false.

Comment by Marcus Speight [ 06/Dec/10 ]

I'm storing settings in a database. using a Namespace, Key => Value triplet. I don't know what might be stored in the value part. Currently strings, NULL and boolean data are stored. Using object seem to be the best approach. (ArrayTypes have the same problem).

If you think a different type would be better then please let me know.

Comment by Benjamin Eberlei [ 06/Dec/10 ]

how is such a triplet only b:0; ? or is your entity the tripplet and the variable that fails is the value?

Comment by Marcus Speight [ 06/Dec/10 ]

I've added my entity. The entity is the triplet. And it is the value that fails if false is stored.
It allows me to store the value(false) but the throws an exception on loading the entity. It should allow the loading of the value(false) or disallow the storing of a false value.

Comment by Benjamin Eberlei [ 12/Dec/10 ]

Although this should really be just array and object, not strange false types i added this patch to prevent future reports of this as a problem. The loosely typed language PHP can go with null or false, so why throw people stones in their way.

Generated at Fri Aug 28 15:25:10 EDT 2015 using JIRA 6.4.10#64025-sha1:5b8b74079161cd76a20ab66dda52747ee6701bd6.