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

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

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

### 映射数据库结构到文件(php\xml\yaml)
[root@localhost ~]# cd /windows/www/symfony_test/
[root@localhost 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
[root@localhost 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"
[root@localhost symfony_test]# 
[root@localhost 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表

[root@localhost 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
[root@localhost 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"

[root@localhost 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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值