基于JSON格式生成Symfony Doctrine2实体

时间:2017-06-26 12:42:56

标签: json symfony doctrine-orm entity

我问自己是否有可能根据json格式生成doctrine2实体。

类似的东西:

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

可能会变成

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

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

问候。

1 个答案:

答案 0 :(得分:2)

我不知道我是否回答了你的问题。 您是否尝试首先将JSON转换为YAML(https://www.json2yaml.com/)? 如果有YML,则可以使用控制台命令

php bin/console generate:doctrine:entities  yourBundle

文档在这里:https://symfony.com/doc/current/doctrine.html#generating-getters-and-setters

例如,有了这个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"
      }
    }
  }
}

你可以推断出这个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

之后,您可以尝试运行此命令:

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