Changes of objects from custom data type are not stored in database #5883

Closed
opened 2026-01-22 15:21:09 +01:00 by admin · 2 comments
Owner

Originally created by @gmponos on GitHub (Feb 14, 2018).

I have a custom type like this

<?php
namespace My\Project\Types;

use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\Type;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;

class JsonParameterBagType extends Type
{
    const JSON_PARAMETER_BAG = 'json_parameter_bag'; // modify to match your type name

    public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
    {
        $platform->getVarcharTypeDeclarationSQL($fieldDeclaration);
    }

    public function convertToPHPValue($value, AbstractPlatform $platform)
    {
        $data = json_decode($value, true);
        return new ParameterBag($data);
    }

    public function convertToDatabaseValue($value, AbstractPlatform $platform)
    {
        return json_encode($value->all());
    }

    public function getName()
    {
        return self::JSON_PARAMETER_BAG; // modify to match your constant name
    }
}

The goal of the above custom type is because I have a field in database that stores values in json I want to convert it to a ParameterBag

When on my entity I do something like this:

$myEntity->getField()->set('my_value', 'value')

and then I save the entity the values of this field are not stored. All the other values are stored normally.

I use doctrine/orm: v2.5.14

Sorry if the issue exists already. I searched about it but haven't found anything.

Originally created by @gmponos on GitHub (Feb 14, 2018). I have a custom type like this ``` php <?php namespace My\Project\Types; use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Types\Type; use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag; class JsonParameterBagType extends Type { const JSON_PARAMETER_BAG = 'json_parameter_bag'; // modify to match your type name public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform) { $platform->getVarcharTypeDeclarationSQL($fieldDeclaration); } public function convertToPHPValue($value, AbstractPlatform $platform) { $data = json_decode($value, true); return new ParameterBag($data); } public function convertToDatabaseValue($value, AbstractPlatform $platform) { return json_encode($value->all()); } public function getName() { return self::JSON_PARAMETER_BAG; // modify to match your constant name } } ``` The goal of the above custom type is because I have a field in database that stores values in json I want to convert it to a [ParameterBag](https://github.com/symfony/symfony/blob/master/src/Symfony/Component/DependencyInjection/ParameterBag/ParameterBag.php) When on my entity I do something like this: ``$myEntity->getField()->set('my_value', 'value')`` and then I save the entity the values of this field are not stored. All the other values are stored normally. I use `doctrine/orm: v2.5.14 ` Sorry if the issue exists already. I searched about it but haven't found anything.
admin closed this issue 2026-01-22 15:21:09 +01:00
Author
Owner

@lcobucci commented on GitHub (Feb 20, 2018):

@gmponos could you please send us a failing test case that reproduces that behaviour? It would help us a lot to identify and fix the issue you're describing.

You can find examples on 388afb46d0/tests/Doctrine/Tests/ORM/Functional/Ticket

@lcobucci commented on GitHub (Feb 20, 2018): @gmponos could you please send us a failing test case that reproduces that behaviour? It would help us a lot to identify and fix the issue you're describing. You can find examples on https://github.com/doctrine/doctrine2/tree/388afb46d0cb3ed0c51332e8df0de9e942c2690b/tests/Doctrine/Tests/ORM/Functional/Ticket
Author
Owner

@beberlei commented on GitHub (Dec 7, 2020):

Changeset computation on types does an identity check, i.e. $oldValue === $newValue. This means you have to replace the whole type with a new instance, you cannot just change the state.

@beberlei commented on GitHub (Dec 7, 2020): Changeset computation on types does an identity check, i.e. $oldValue === $newValue. This means you have to replace the whole type with a new instance, you cannot just change the state.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#5883