天天看点

Symfony3.x 通过数据库反向生成entity

Symfony3.x 通过过数据库反向生成entity

参考文档: http://symfony.cn/docs/cookbook/doctrine/reverse_engineering.html

### 映射数据库结构到文件(php\xml\yaml)
[[email protected] ~]# cd /windows/www/symfony_test/
[[email protected] symfony_test]# php bin/console  doctrine:mapping:import --force AppBundle php
Importing mapping information from "default" entity manager
  > writing /windows/www/symfony_test/src/AppBundle/Resources/config/doctrine/Feedback.orm.php
  > writing /windows/www/symfony_test/src/AppBundle/Resources/config/doctrine/User.orm.php
  > writing /windows/www/symfony_test/src/AppBundle/Resources/config/doctrine/User.orm.php



### 生成entity
[[email protected] symfony_test]# php bin/console doctrine:mapping:convert annotation ./src

 Processing entity "AppBundle\Entity\Feedback"
 Processing entity "AppBundle\Entity\User"
 Processing entity "AppBundle\Entity\User"

 Exporting "annotation" mapping information to "/windows/www/symfony_test/src"
[[email protected] symfony_test]# 
[[email protected] symfony_test]# tree src/
src/
└── AppBundle
    ├── AppBundle.php
    ├── Controller
    │   ├── DefaultController.php
    │   └── UserController.php
    ├── Entity
    │   └── User.php
    ├── Repository
    │   ├── DemoRepository.php
    │   └── UserRepository.php
    ├── Resources
    │   └── config
    │       └── doctrine
    │           ├── User.orm.php
    └── Serverice
        └── UserService.php
           
  1. entity生成后,需要自己使用IDE工具生成getters和setters方法, 以及一些关联关系, 验证Assert提示信息
  2. 注意entity定义字段的格式,@var string|null必须要写,不然会找不到该字段

为新增的table生成entity类文件

新创建一张users表和 banks表

[[email protected] symfony_test]# php bin/console doctrine:mapping:import AppBundle yml --filter=Users --filter=Banks
Importing mapping information from "default" entity manager
  > writing /windows/www/symfony_test/src/AppBundle/Resources/config/doctrine/Banks.orm.yml
  > writing /windows/www/symfony_test/src/AppBundle/Resources/config/doctrine/Users.orm.yml
[[email protected] symfony_test]# php bin/console doctrine:mapping:convert annotation ./src --filter=Users

 Processing entity "AppBundle\Entity\Users"
 Exporting "annotation" mapping information to "/windows/www/symfony_test/src"

[[email protected] symfony_test]# php bin/console doctrine:generate:entities AppBundle/Entity/Users
Generating entity "AppBundle\Entity\Users"
  > backing up Users.php to Users.php~
  > generating AppBundle\Entity\Users
           
  1. 生成ORM映射文件的格式必须是yml或者xml
  2. 可以通过–filter指定需要生成的映射文件(A string pattern used to match entities that should be mapped. (multiple values allowed)) 因此–filter必须和entities匹配, 即必须是类名。
  3. 生成entities文件的时候,仍然可以通过–filter指定需要生成的entity类。
  4. 通过php bin/console doctrine:generate:entities命令生成get set 方法, 可以添加参数AppBundle\Entity\Users 来指定需要操作的类(php bin/console doctrine:generate:entities –help查看帮助信息)

Entity格式

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * User
 *
 * @ORM\Table(name="user")
 * @ORM\Entity
 * @ORM\Entity(repositoryClass="AppBundle\Repository\UserRepository")
 */
class User
{
    /**
     * @var string|null
     *
     * @ORM\Column(name="name", type="string", length=100, nullable=true)
     */
    private $name;

    /**
     * @var bool|null
     *
     * @ORM\Column(name="enabled", type="boolean", nullable=true, options={"default"="1"})
     */
    private $enabled = '1';

    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;
}