Learn what DC2Type strings stored with Doctrine in your Database mean and how to unserialize them.

Not any Database engine, like MySQL knows what PHP serialization is, so you can't do this at this level which means, that you will need to do it yes or yes with PHP. The DC2Type fields in doctrine is stored as a field with LONGTEXT type, as MySQL doesn't allow to store arrays in the database but strings. An example of the data (string) that Doctrine stores in a register in your database looks as follows:

a:15:{
    i:0;s:32:"ROLE_SONATA_USER_ADMIN_USER_EDIT";
    i:1;s:32:"ROLE_SONATA_USER_ADMIN_USER_LIST";
    i:2;s:34:"ROLE_SONATA_USER_ADMIN_USER_CREATE";
    i:3;s:32:"ROLE_SONATA_USER_ADMIN_USER_VIEW";
    i:4;s:34:"ROLE_SONATA_USER_ADMIN_USER_DELETE";
    i:5;s:36:"ROLE_SONATA_USER_ADMIN_USER_OPERATOR";
    i:6;s:34:"ROLE_SONATA_USER_ADMIN_USER_MASTER";
    i:7;s:33:"ROLE_SONATA_USER_ADMIN_GROUP_EDIT";
    i:8;s:33:"ROLE_SONATA_USER_ADMIN_GROUP_LIST";
    i:9;s:35:"ROLE_SONATA_USER_ADMIN_GROUP_CREATE";
    i:10;s:33:"ROLE_SONATA_USER_ADMIN_GROUP_VIEW";
    i:11;s:35:"ROLE_SONATA_USER_ADMIN_GROUP_DELETE";
    i:12;s:37:"ROLE_SONATA_USER_ADMIN_GROUP_OPERATOR";
    i:13;s:35:"ROLE_SONATA_USER_ADMIN_GROUP_MASTER";
    i:14;s:10:"ROLE_ADMIN";
}

You may think initially that this must be a complex format and that can be only decoded with Doctrine, however this string is just a normal serialized array created by the serialize function of PHP. Think of this as another notation to represent information like JSON or XML.

How to decode (convert to PHP array)

If you are working with doctrine and you are interacting with the data as Entities, then usually if the field type is already specified in Doctrine, the getter property of the entity should return an array. However, if you aren't using Doctrine but plain PHP you can convert the string into an array using the deserialize function of PHP. The PHP function serialize serializes a data structure into a string representation that's unique to PHP:

$dc2array = serialize(array("HELLO", "YES"));

// Output: string(36) "a:2:{i:0;s:5:"HELLO";i:1;s:3:"YES";}" 
var_dump($dc2array);

and can be reversed into a PHP object using unserialize:

$dc2array = unserialize('a:15:{i:0;s:32:"ROLE_SONATA_USER_ADMIN_USER_EDIT";i:1;s:32:"ROLE_SONATA_USER_ADMIN_USER_LIST";i:2;s:34:"ROLE_SONATA_USER_ADMIN_USER_CREATE";i:3;s:32:"ROLE_SONATA_USER_ADMIN_USER_VIEW";i:4;s:34:"ROLE_SONATA_USER_ADMIN_USER_DELETE";i:5;s:36:"ROLE_SONATA_USER_ADMIN_USER_OPERATOR";i:6;s:34:"ROLE_SONATA_USER_ADMIN_USER_MASTER";i:7;s:33:"ROLE_SONATA_USER_ADMIN_GROUP_EDIT";i:8;s:33:"ROLE_SONATA_USER_ADMIN_GROUP_LIST";i:9;s:35:"ROLE_SONATA_USER_ADMIN_GROUP_CREATE";i:10;s:33:"ROLE_SONATA_USER_ADMIN_GROUP_VIEW";i:11;s:35:"ROLE_SONATA_USER_ADMIN_GROUP_DELETE";i:12;s:37:"ROLE_SONATA_USER_ADMIN_GROUP_OPERATOR";i:13;s:35:"ROLE_SONATA_USER_ADMIN_GROUP_MASTER";i:14;s:10:"ROLE_ADMIN";}');

var_dump($dc2array);

The var_dump method will print the structure of the array that will be the following:

array(15) { 
    [0]=> string(32) "ROLE_SONATA_USER_ADMIN_USER_EDIT" 
    [1]=> string(32) "ROLE_SONATA_USER_ADMIN_USER_LIST" 
    [2]=> string(34) "ROLE_SONATA_USER_ADMIN_USER_CREATE" 
    [3]=> string(32) "ROLE_SONATA_USER_ADMIN_USER_VIEW" 
    [4]=> string(34) "ROLE_SONATA_USER_ADMIN_USER_DELETE" 
    [5]=> string(36) "ROLE_SONATA_USER_ADMIN_USER_OPERATOR" 
    [6]=> string(34) "ROLE_SONATA_USER_ADMIN_USER_MASTER" 
    [7]=> string(33) "ROLE_SONATA_USER_ADMIN_GROUP_EDIT" 
    [8]=> string(33) "ROLE_SONATA_USER_ADMIN_GROUP_LIST" 
    [9]=> string(35) "ROLE_SONATA_USER_ADMIN_GROUP_CREATE" 
    [10]=> string(33) "ROLE_SONATA_USER_ADMIN_GROUP_VIEW" 
    [11]=> string(35) "ROLE_SONATA_USER_ADMIN_GROUP_DELETE" 
    [12]=> string(37) "ROLE_SONATA_USER_ADMIN_GROUP_OPERATOR" 
    [13]=> string(35) "ROLE_SONATA_USER_ADMIN_GROUP_MASTER" 
    [14]=> string(10) "ROLE_ADMIN" 
}

Happy coding !


Senior Software Engineer at Software Medico. Interested in programming since he was 14 years old, Carlos is a self-taught programmer and founder and author of most of the articles at Our Code World.

Sponsors