dunglas/doctrine-json-odm

Deserialize nested object

pierrejolly opened this issue · 3 comments

Hello,

When I save object with nested object everything works properly and insertion in DB is correct (object is well serialized), but when I want to get my data the nested object wasn't deserialized. Is someone as any idea on how to achieve that ?

Example :

class Foo
{
    /**
     * ...
     */

    /**
     * @ORM\Column(name="configuration", type="json_document", nullable=true)
     */
    public $configuration;

    /**
     * Get configuration.
     */
    public function getConfiguration()
    {
        return $this->configuration;
    }

    /**
     * Set configuration.
     */
    public function setConfiguration($configuration)
    {
        $this->configuration = $configuration;
    }
}

class FooConfiguration
{
    public $bar;
    public $date;
}

Usage

$foo = new Foo();

$foo = new FooConfiguration()
$foo->bar = 'test';
$foo->date = new \Datetime();

$foo->setConfiguration($fooConfiguration);

$em->persist($foo);
$em->flush();

On insertion my date is well serialized but I can't get my \DateTime object back on deserialization

Thank you for your time.

It is recursive. However, if there are normalizers missing for certain attributes, these may be ignored. E.g. DateTime is not supported by default, see https://github.com/dunglas/doctrine-json-odm#faq.

@Toflar thanks, I already added the serializer like described in the documentation and my \Datetime is well serialized, in database I have the following value for FooConfiguration :

{
    "foo": "test",
    "date": "2014-01-01T00:00:00+01:00",
    "#type": "App\\Entity\\FooConfiguration"
}

Everything is ok for me until there.

But when I get the FooConfiguration from DB, datetime is not unserialized to a \DateTime object

$fooConfiguration = $foo->getConfiguration();
dump($fooConfiguration);

Capture d’écran 2019-04-10 à 16 51 32

Then of course when I want to use this in my form with a DateType for element I got the following error :

Unable to transform value for property path "date": Expected a \DateTimeInterface.

Problem solved, thank you for you time :).
It's only because I used a public property. I didn't know that serializer use type hint to unserialize data.