JMSSerializerBundle Yaml config for custom entity class

2k views Asked by At

I've got in my symfony2 project, a custom entity outside Bundles and framework.

I need set json data into this entity, but i can't apply right configuration to user yaml file.

app/config/config.yml

jms_serializer:
    metadata:
        auto_detection: true
        directories:
            CORE:
                namespace_prefix: "Core\Domain\Model"
                path: "%kernel.root_dir%/Resources/serializer/CORE"

app/Resources/serializer/CORE/Model.Product.yml

Core\Domain\Model\Product\Product:
    properties:
        id:
            type: integer
        objectId:
            type: string
        name:
            type: string ...

It's possible that this bundle dont works fine with entities outside bundles?.

Always I see error message: You must define a type for Core\Domain\Model\Product\Product::$id.

I think that JMSSerializerBundle don't read yaml file, because with annotations works fine.

Any idea?.

Thanks.

1

There are 1 answers

4
Fabian Kleiser On

In your app/config/config.yml be sure to use \\ as namespace separator instead of \:

jms_serializer:
    metadata:
        auto_detection: true
        directories:
            CORE:
                namespace_prefix: "Core\\Domain\\Model"
                path: "%kernel.root_dir%/Resources/serializer/CORE"

Otherwise the backslashes are treated as escape characters for the following letters.

Edit: Also make sure to name the JMS serializer config properly. For the class Core\Domain\Model\Product\Product you need a Product.Product.yml file inside the specified path of the config. In your example, your file is named Model.Product.yml.

So to get the serializer config file name for an entitiy in general:

  1. Strip the namespace defined in the config from the class name
  2. replace the namespace separators \ with .
  3. append .yml and put the file in the path folder defined in the config