dunglas/doctrine-json-odm

Must be available. Call the "setSerializer" method.

DanielStudioSystems opened this issue · 11 comments

Hello,
we encounter a problem on our project:
An instance of "Symfony\Component\Serializer\SerializerInterface" must be available. Call the "setSerializer" method.
We followed the installation steps, but even on a new project we have the same error.

Here is a test project:

https://github.com/DanielStudioSystems/doctrine-json-odm-exemple
For the quick test we used a bdd with the Foo entity
Then create a controller with the route \ test

Here is the error message:

image

Can you help us ?

The bundle is missing in your bundles.php. This is where the serializer gets set (https://github.com/dunglas/doctrine-json-odm/blob/master/src/Bundle/DunglasDoctrineJsonOdmBundle.php#L21).
Please follow the installation instructions in the readme :)

Thank you for your quick reply !
In the readme.md we had seen:

If you are using Symfony 4+ or API Platform, you have nothing more to do!  
If you use Doctrine directly, use a bootstrap code similar to the following:

We use Symfony 4.2 and API Platform so we stopped here.

For information it works like this:

// config/bundles.php
<?php
return [
    Symfony\Bundle\FrameworkBundle\FrameworkBundle::class => ['all' => true],
    // ... ,
    \Dunglas\DoctrineJsonOdm\Bundle\DunglasDoctrineJsonOdmBundle::class => ['dev' => true],
];

It only works with "dev", "all" does not work.
thanks again !

Are you using Symfony 4 without Flex? Because it updates your bundles.php automatically that‘s why the docs say you don‘t need to do anything else. Also, only in dev environment is likely not what you want because it would be missing in production :D

After use the command composer show :
symfony/finder v4.2.4 Symfony Finder Component
symfony/flex v1.2.0 Composer plugin for Symfony
symfony/form v4.2.4 Symfony Form Component

So we have the v1.2.0

I copy / paste the composer.json from my project to be in the same conditions.
I noticed that several bundle was delete.
But I have symfony / flex to install

Here my log after a update :

Updating dependencies (including require-dev)
Restricting packages listed in "symfony/symfony" to "4.2.*"
Package operations: 41 installs, 8 updates, 10 removals
  - Removing symfony/web-server-bundle (v4.2.4)
  - Removing symfony/swiftmailer-bundle (v3.2.5)
  - Removing symfony/serializer-pack (v1.0.2)
  - Removing symfony/polyfill-intl-idn (v1.10.0)
  - Removing symfony/orm-pack (v1.0.6)
  - Removing symfony/debug-pack (v1.0.7)
  - Removing symfony/debug-bundle (v4.2.4)
  - Removing swiftmailer/swiftmailer (v6.2.0)
  - Removing egulias/email-validator (2.1.7)
  - Removing easycorp/easy-log-handler (v1.0.7)
  - Updating symfony/polyfill-mbstring (v1.10.0 => v1.11.0): Loading from cache
  - Updating doctrine/annotations (v1.6.0 => v1.6.1): Loading from cache
  - Downgrading doctrine/migrations (v2.0.0 => v1.8.1): Loading from cache
  - Downgrading doctrine/doctrine-migrations-bundle (v2.0.0 => v1.3.2): Loading from cache
  - Updating doctrine/instantiator (1.1.0 => 1.2.0): Loading from cache
  - Updating symfony/polyfill-php72 (v1.10.0 => v1.11.0): Loading from cache
  - Updating symfony/polyfill-intl-icu (v1.10.0 => v1.11.0): Loading from cache
  - Updating twig/twig (v2.7.2 => v2.7.4): Loading from cache
  - Installing nelmio/cors-bundle (1.5.5): Loading from cache
  - Installing willdurand/negotiation (v2.3.1): Loading from cache
  - Installing api-platform/core (2.4.x-dev c1482be): Loading from cache
  - Installing api-platform/api-pack (v1.2.0): Loading from cache
  - Installing dunglas/doctrine-json-odm (0.1.3): Loading from cache
  - Installing ralouphie/getallheaders (2.0.5): Loading from cache
  - Installing psr/http-message (1.0.1): Loading from cache
  - Installing guzzlehttp/psr7 (1.5.2): Loading from cache
  - Installing php-http/discovery (1.6.1): Loading from cache
  - Installing clue/stream-filter (v1.4.0): Loading from cache
  - Installing php-http/message-factory (v1.0.2): Loading from cache
  - Installing php-http/message (1.7.2): Loading from cache
  - Installing php-http/promise (v1.0.0): Loading from cache
  - Installing php-http/httplug (v1.1.0): Loading from cache
  - Installing php-http/curl-client (v1.7.1): Loading from cache
  - Installing php-http/client-common (1.9.1): Loading from cache
  - Installing php-http/stopwatch-plugin (1.2.0): Loading from cache
  - Installing php-http/logger-plugin (1.1.0): Loading from cache
  - Installing php-http/cache-plugin (1.6.0): Loading from cache
  - Installing php-http/httplug-bundle (1.14.0): Loading from cache
  - Installing php-http/httplug-pack (1.1.0): Loading from cache
  - Installing phpmailer/phpmailer (v5.2.27): Loading from cache
  - Installing container-interop/container-interop (1.2.0): Loading from cache
  - Installing symfony/class-loader (v3.4.23): Loading from cache
  - Installing behat/transliterator (v1.2.0): Loading from cache
  - Installing behat/gherkin (v4.6.0): Loading from cache
  - Installing behat/behat (v3.5.0): Loading from cache
  - Installing beberlei/doctrineextensions (v1.2.0): Loading from cache
  - Installing namshi/jose (7.2.3): Loading from cache
  - Installing lcobucci/jwt (3.2.5): Loading from cache
  - Installing lexik/jwt-authentication-bundle (v2.6.1): Loading from cache
  - Installing composer/ca-bundle (1.1.4): Loading from cache
  - Installing sensiolabs/security-checker (v5.0.3): Loading from cache
  - Installing sentry/sentry (1.10.0): Loading from cache
  - Installing symfony/templating (v4.2.4): Loading from cache
  - Installing jms/metadata (1.7.0): Loading from cache
  - Installing vich/uploader-bundle (1.8.6): Loading from cache
  - Installing webonyx/graphql-php (v0.13.1): Loading from cache
  - Installing doctrine/data-fixtures (v1.3.1): Loading from cache
  - Installing doctrine/doctrine-fixtures-bundle (3.1.0): Loading from cache
Writing lock file
Generating autoload files
ocramius/package-versions:  Generating version class...
ocramius/package-versions: ...done generating version class

What about running composer global require symfony/thanks && composer thanks now?
This will spread some 💖  by sending a ★  to the GitHub repositories of your fellow package maintainers.

Symfony operations: 12 recipes (828691f8df8eff0cdf48f9f34fe709cb)
  - Unconfiguring symfony/web-server-bundle (>=3.3): From github.com/symfony/recipes:master
  - Unconfiguring symfony/swiftmailer-bundle (>=2.5): From github.com/symfony/recipes:master
  - Unconfiguring symfony/debug-bundle (>=4.1): From github.com/symfony/recipes:master
  - Unconfiguring easycorp/easy-log-handler (>=1.0): From github.com/symfony/recipes:master
  - Configuring nelmio/cors-bundle (>=1.5): From github.com/symfony/recipes:master
  - Configuring api-platform/core (>=2.1): From github.com/symfony/recipes:master
  -  WARNING  dunglas/doctrine-json-odm (>=0.1): From github.com/symfony/recipes-contrib:master
    The recipe for this package comes from the "contrib" repository, which is open to community contributions.
    Review the recipe at https://github.com/symfony/recipes-contrib/tree/master/dunglas/doctrine-json-odm/0.1

    Do you want to execute this recipe?
    [y] Yes
    [n] No
    [a] Yes for all packages, only for the current installation session
    [p] Yes permanently, never ask again for this project
    (defaults to n): 
  - Configuring php-http/httplug-bundle (>=1.6): From github.com/symfony/recipes:master
  - Configuring lexik/jwt-authentication-bundle (>=2.5): From github.com/symfony/recipes:master
  - Configuring sensiolabs/security-checker (>=4.0): From github.com/symfony/recipes:master
  -  WARNING  vich/uploader-bundle (>=1.5): From github.com/symfony/recipes-contrib:master
    The recipe for this package comes from the "contrib" repository, which is open to community contributions.
    Review the recipe at https://github.com/symfony/recipes-contrib/tree/master/vich/uploader-bundle/1.5

    Do you want to execute this recipe?
    [y] Yes
    [n] No
    [a] Yes for all packages, only for the current installation session
    [p] Yes permanently, never ask again for this project
    (defaults to n): 
  - Configuring doctrine/doctrine-fixtures-bundle (>=3.0): From github.com/symfony/recipes:master
Executing script cache:clear [OK]
Executing script assets:install public [OK]
Executing script security-checker security:check [OK]

Some files may have been created or updated to configure your new packages.
Please review, edit and commit them: these files are yours.

I miss something ?

WARNING  dunglas/doctrine-json-odm (>=0.1): From github.com/symfony/recipes-contrib:master
    The recipe for this package comes from the "contrib" repository, which is open to community contributions.
    Review the recipe at https://github.com/symfony/recipes-contrib/tree/master/dunglas/doctrine-json-odm/0.1

    Do you want to execute this recipe?

If you answer this with a y, the bundle should get automatically registered in your bundles.php

Thanks for your help,
I remove and require again dunglas/doctrine-json-odm

This time I answered y
You're right this automatically registered in my bundles.php :

// config/bundles.php
<?php
return [
    // ... ,
    Dunglas\DoctrineJsonOdm\Bundle\DunglasDoctrineJsonOdmBundle::class => ['all' => true],
];

But the generated code does not work, I have the same error :
An instance of "Symfony\Component\Serializer\SerializerInterface" must be available. Call the "setSerializer" method.

I also install symfony / serializer-pack because I saw that it was uninstalling.
it does not work.

I used the symfony command to empty the cache:
php bin/console cache: clear
Same result with the error.
But by deleting the folder var/dev
The first time it works then if I update my page the error appears ...

I agree with you the mistake must come from my configuration.
But I created a project just for the test. The only mistake that should happen would be on the composer.json?

Edit :
I check the register bundle with:
php bin/console config:dump-reference
I see :
DunglasDoctrineJsonOdmBundle dunglas_doctrine_json_odm

So did anyone managed to fix it? I have the same problem and no solution so far.

I have not found a solution with this bundle, I used the latest version of doctrine which allows recorded from an array of json in database:

class myClass {
    / **
     * @var Array | null
     * @ORM \ Column (type = "json")
     * /
    protected $data;

    / **
     * @return Array | null
     * /
    public function getData ():? Array
    {
        return $this-> data;
    }

    / **
     * @param Array | null $data
     *
     * @return MyClass
     * /
    public function setData (? Array $data): MyClass
    {
        $this-> data = $ data;

        return $this;
    }
}

Ok, I solved my issue. In my case I unnecessarily added this to config in config/packages/doctrine.yaml:

doctrine:
    dbal:
        types:
            json_document: Dunglas\DoctrineJsonOdm\Type\JsonDocumentType

So deleting this line fixed it for me.