dunglas/doctrine-json-odm

POPO column not persisting

cabdesigns opened this issue · 1 comments

Based on the docs, I'm of the understanding that I should be able to create a stdClass object as a property on an Entity, and this will be persisted as json. However, what I'm finding, is this object gets written into the DB (postgres) like so:

{"#type":"stdClass"}

All properties added to the stdClass object seem to disappear.

I've var_dumped out the Entity just to sanity check I have indeed set the data as expected, just before persisting - and it does look to be correct at this point.

If I json_encode the object first, it successfully writes the json, but as a string which is not desirable and misses the point of using this bundle. Based on that, I can only assume I've misunderstood the capabilities of this bundle, discovered a bug, or incorrectly configured it.

Here's what the entity looks like:

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass="App\Repository\AccountConfigRepository")
 */
class AccountConfig
{
    /**
     * @ORM\Id()
     * @ORM\Column(type="string", length=255)
     */
    private $uuid;

    /**
     * @ORM\Column(type="json_document", options={"jsonb": true})
     */
    private $config;

    public function getUuid(): ?string
    {
        return $this->uuid;
    }

    public function setUuid(string $uuid): self
    {
        $this->uuid = $uuid;
        return $this;
    }

    public function getConfig()
    {
        return $this->config;
    }

    public function setConfig($config): self
    {
        $this->config = $config;
        return $this;
    }
}

And here's an example of the calling code:

                $config = (object)[
                    'colour' => 'blue',
                    'phone' => '00000000000',
                ];

                $accountConfig = new AccountConfig();
                $accountConfig->setUuid($uuid);
                $accountConfig->setConfig($config);

                $entityManager->persist($accountConfig);
                $entityManager->flush();

In terms of wiring up the bundle, I'm using Symfony 4, and I've just added one line to config/bundles.php:

<?php

return [
    ...
    Dunglas\DoctrineJsonOdm\Bundle\DunglasDoctrineJsonOdmBundle::class =>  ['all' => true],
];

Any ideas? Thanks.

Ok, figured it out. The serializer doesn't appear to like stdClass very much. Setting as an array worked (as per example in docs):

$config = [
    'colour' => 'blue',
    'phone' => '00000000000',
];