fxlacroix
fxlacroix

Reputation: 567

Generate Symfony Doctrine2 Entity based on JSON format

I'm asking myself if it could be possible to generate doctrine2 Entity based on json format.

something like this :

"address": {
        "postal_code": "91512"
},

could became

/**
 * @ORM\Entity
 * @ORM\Table(name="Adress")
 */
class Adress{

    /**
     * @var string // ideal should be integer
     */
    protected $postalCode;
}

regards.

Upvotes: 2

Views: 1124

Answers (1)

Arno
Arno

Reputation: 1359

I don't know if I answer your question. Have you try to first convert JSON to YAML (https://www.json2yaml.com/) ? When you have YML, you can use the console command

php bin/console generate:doctrine:entities  yourBundle

A documentation is here : https://symfony.com/doc/current/doctrine.html#generating-getters-and-setters

For example, with this Json:

{
  "AppBundle\\Entity\\Product": {
    "type": "entity",
    "table": "product",
    "id": {
      "id": {
        "type": "integer",
        "generator": {
          "strategy": "AUTO"
        }
      }
    },
    "fields": {
      "name": {
        "type": "string",
        "length": 100
      },
      "price": {
        "type": "decimal",
        "scale": 2
      },
      "description": {
        "type": "text"
      }
    }
  }
}

You can deduce this Yaml :

# src/AppBundle/Resources/config/doctrine/Product.orm.yml
AppBundle\Entity\Product:
    type: entity
    table: product
    id:
        id:
            type: integer
            generator: { strategy: AUTO }
    fields:
        name:
            type: string
            length: 100
        price:
            type: decimal
            scale: 2
        description:
            type: text

After that, you can try to run this command :

php bin/console doctrine:generate:entities AppBundle/Entity/Product

Upvotes: 2

Related Questions