DDC-3241: object type fails to save serialized class to postgresql #4011

Open
opened 2026-01-22 14:33:39 +01:00 by admin · 12 comments
Owner

Originally created by @doctrinebot on GitHub (Aug 5, 2014).

Jira issue originally created by user renoreckling:

Doctrine 2 fails to properly store data from serialize() into postgresql.
This happens because the postgresql pdo driver truncates text on NULL bytes when escaping values. This leads to truncated serialized objects being inserted into the database.

A ugly but working workaround for us is to call json_encode(serialize()) when saving to the database and unserialize(json_decode()) when reading the value back because json_encode properly serializes the NULL bytes of the serialize() output to readable text.

This is pretty ugly though and it would help if doctrine would provide a minimal encoding/decoding function for postgresql that converts all NULL bytes to something else to not break the object type on postgresql.

Originally created by @doctrinebot on GitHub (Aug 5, 2014). Jira issue originally created by user renoreckling: Doctrine 2 fails to properly store data from serialize() into postgresql. This happens because the postgresql pdo driver truncates text on NULL bytes when escaping values. This leads to truncated serialized objects being inserted into the database. A ugly but working workaround for us is to call json_encode(serialize()) when saving to the database and unserialize(json_decode()) when reading the value back because json_encode properly serializes the NULL bytes of the serialize() output to readable text. This is pretty ugly though and it would help if doctrine would provide a minimal encoding/decoding function for postgresql that converts all NULL bytes to something else to not break the object type on postgresql.
admin added the BugMissing Tests labels 2026-01-22 14:33:39 +01:00
Author
Owner

@doctrinebot commented on GitHub (Aug 5, 2014):

@doctrinebot commented on GitHub (Aug 5, 2014): - relates to [DBAL-964: [GH-653] Update docs to include warning when using object type with PostgreSQL](http://www.doctrine-project.org/jira/browse/DBAL-964)
Author
Owner

@doctrinebot commented on GitHub (Aug 5, 2014):

Comment created by @ocramius:

I'm fairly sure that we don't want to invent our own serialization format. Instead, base64_encode could work.

Due to BC compat, we can't introduce it for 2.x, so it would have to be a completely new type.

@doctrinebot commented on GitHub (Aug 5, 2014): Comment created by @ocramius: I'm fairly sure that we don't want to invent our own serialization format. Instead, `base64_encode` could work. Due to BC compat, we can't introduce it for 2.x, so it would have to be a completely new type.
Author
Owner

@doctrinebot commented on GitHub (Aug 5, 2014):

Comment created by renoreckling:

Agreed, we'll just go with base64_encode for now.
Maybe someone should update the documentation that using the object type on postgresql is not working as well as that using binary strings containing a NULL byte in any varchar/text column on postgresql is not working either.

@doctrinebot commented on GitHub (Aug 5, 2014): Comment created by renoreckling: Agreed, we'll just go with base64_encode for now. Maybe someone should update the documentation that using the object type on postgresql is _not_ working as well as that using binary strings containing a NULL byte in any varchar/text column on postgresql is not working either.
Author
Owner

@doctrinebot commented on GitHub (Aug 5, 2014):

Comment created by @ocramius:

[~renoreckling] just open a pull request against the doctrine/doctrine2 repository

@doctrinebot commented on GitHub (Aug 5, 2014): Comment created by @ocramius: [~renoreckling] just open a pull request against the doctrine/doctrine2 repository
Author
Owner

@doctrinebot commented on GitHub (Aug 5, 2014):

Comment created by renoreckling:

[~ocramius] Here you go: https://github.com/doctrine/dbal/pull/653
http://www.doctrine-project.org/jira/browse/DBAL-964

Thanks for the reply.

@doctrinebot commented on GitHub (Aug 5, 2014): Comment created by renoreckling: [~ocramius] Here you go: https://github.com/doctrine/dbal/pull/653 http://www.doctrine-project.org/jira/browse/[DBAL-964](http://www.doctrine-project.org/jira/browse/DBAL-964) Thanks for the reply.
Author
Owner

@doctrinebot commented on GitHub (Aug 5, 2014):

Comment created by @doctrinebot:

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

@doctrinebot commented on GitHub (Aug 5, 2014): Comment created by @doctrinebot: A related Github Pull-Request [GH-653] was closed: https://github.com/doctrine/dbal/pull/653
Author
Owner

@doctrinebot commented on GitHub (Aug 5, 2014):

Comment created by @ocramius:

Merged DBAL-964, but the issue persists here.

@doctrinebot commented on GitHub (Aug 5, 2014): Comment created by @ocramius: Merged [DBAL-964](http://www.doctrine-project.org/jira/browse/DBAL-964), but the issue persists here.
Author
Owner

@root-talis commented on GitHub (Dec 4, 2017):

I've just ran into this bug. Any hope this will be fixed? Right now a workaround is to do base64_encode(serialize($field)), as suggested above, but it doesn't look like a good way to go.

@root-talis commented on GitHub (Dec 4, 2017): I've just ran into this bug. Any hope this will be fixed? Right now a workaround is to do `base64_encode(serialize($field))`, as suggested above, but it doesn't look like a good way to go.
Author
Owner

@zeromodule commented on GitHub (Dec 6, 2017):

I am also interested in solving this problem.
This bug is 3 years old.
Does it make sense to wait for a fix in the near future?

@zeromodule commented on GitHub (Dec 6, 2017): I am also interested in solving this problem. This bug is 3 years old. Does it make sense to wait for a fix in the near future?
Author
Owner

@Ocramius commented on GitHub (Dec 6, 2017):

Requires:

  1. a test case
  2. a fix

Don't expect core maintainers to fix this for you, sorry.

@Ocramius commented on GitHub (Dec 6, 2017): Requires: 1. a test case 2. a fix Don't expect core maintainers to fix this for you, sorry.
Author
Owner

@NeoFusion commented on GitHub (Jan 28, 2019):

My solution: create new mapping type based on ObjectType

<?php

namespace AppBundle\DBAL\Types;

use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\ObjectType;

class ByteObjectType extends ObjectType
{
    public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
    {
        return $platform->getBlobTypeDeclarationSQL($fieldDeclaration);
    }

    public function convertToDatabaseValue($value, AbstractPlatform $platform)
    {
        return pg_escape_bytea(serialize($value));
    }

    public function getName()
    {
        return 'byte_object';
    }
}
doctrine:
    dbal:
        types:
            byte_object: AppBundle\DBAL\Types\ByteObjectType
/**
 * @ORM\Column(type="byte_object")
 */
@NeoFusion commented on GitHub (Jan 28, 2019): My solution: create new mapping type based on `ObjectType` ```php <?php namespace AppBundle\DBAL\Types; use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Types\ObjectType; class ByteObjectType extends ObjectType { public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform) { return $platform->getBlobTypeDeclarationSQL($fieldDeclaration); } public function convertToDatabaseValue($value, AbstractPlatform $platform) { return pg_escape_bytea(serialize($value)); } public function getName() { return 'byte_object'; } } ``` ```yaml doctrine: dbal: types: byte_object: AppBundle\DBAL\Types\ByteObjectType ``` ```php /** * @ORM\Column(type="byte_object") */ ```
Author
Owner

@zlikavac32 commented on GitHub (Mar 11, 2020):

@NeoFusion I stumbled upon the same issue and solved it similarly. But instead of convertToDatabaseValue(), I used

public function getBindingType(): int
{
    return ParameterType::LARGE_OBJECT;
}
@zlikavac32 commented on GitHub (Mar 11, 2020): @NeoFusion I stumbled upon the same issue and solved it similarly. But instead of `convertToDatabaseValue()`, I used ```php public function getBindingType(): int { return ParameterType::LARGE_OBJECT; } ```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#4011