2

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.

1
  • 1
    yes it is possible. You need to provide some more information about where you want to achieve this (e.g. in a controller). Please check out the ParamConverter documentation included in the SensioFrameworkExtraBundle Commented Jun 26, 2017 at 13:11

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.