在我的以前一章Symfony之十分钟入门说了怎样生成数据库,然后设计实体Entity,再同步数据库的表结构,一般我们的顺序都是这样:生成数据库->设计实体Entity->同步数据库表结构。

但是如果你想要在设计Entity前,先自己创建数据库和表结构,再生成Entity;或者是在设计Entity,同步数据库表结构之后,Entity文件全部误删丢失想找回来。下面介绍方法。

我们来做一个例子:

1.创建两张表:section,article

2.生成.orm.xml文件

$ php app/console doctrine:mapping:import --force SiteHomeBundle xml

这个命令行让Doctrine检查数据库并生成XML元数据文件到src/Site/HomeBundle/Resources/config/doctrine文件夹下。
新生成了两个文件:

Section.orm.xml

<?xml version="1.0" encoding="utf-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
<entity name="Site\HomeBundle\Entity\Section" table="section">
<id name="id" type="integer" column="id">
<generator strategy="IDENTITY"/>
</id>
<field name="name" type="string" column="name" length="20" nullable="false"/>
</entity>
</doctrine-mapping>

Article.orm.xml

<?xml version="1.0" encoding="utf-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
<entity name="Site\HomeBundle\Entity\Article" table="article">
<id name="id" type="integer" column="id">
<generator strategy="IDENTITY"/>
</id>
<field name="title" type="string" column="title" length="20" nullable="false"/>
<field name="content" type="text" column="content" nullable="false"/>
<field name="author" type="string" column="author" length="10" nullable="false"/>
</entity>
</doctrine-mapping>

3.生成实体Entity

元数据文件生成后,可以用下面的命令来构建相关的实体Entity

1
2
$ php app/console doctrine:mapping:convert annotation ./src
$ php app/console doctrine:generate:entities SiteHomeBundle --no-backup

第一个命令生成annotation注释映射的实体Entity类;
第二个命令生成整个SiteHomeBundle下每个Entity类的get,set方法
但是,如果你想使用YAML或XML代替annotation映射注释,你只需要执行第二个命令。
如果你想使用annotations,你可以在使用这两个命令之后安全的删除.orm.xml文件。
现在两个实体Entity就生成成功了。

在src/Site/HomeBundle/Entity文件夹下

Section.php

<?php

namespace Site\HomeBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
* Section
*
* @ORM\Table(name="section")
* @ORM\Entity
*/
class Section
{
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=20, nullable=false)
*/
private $name; /**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id; /**
* Set name
*
* @param string $name
* @return Section
*/
public function setName($name)
{
$this->name = $name; return $this;
} /**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
} /**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
}

Article.php

<?php

namespace Site\HomeBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
* Article
*
* @ORM\Table(name="article")
* @ORM\Entity
*/
class Article
{
/**
* @var string
*
* @ORM\Column(name="title", type="string", length=20, nullable=false)
*/
private $title; /**
* @var string
*
* @ORM\Column(name="content", type="text", nullable=false)
*/
private $content; /**
* @var string
*
* @ORM\Column(name="author", type="string", length=10, nullable=false)
*/
private $author; /**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id; /**
* Set title
*
* @param string $title
* @return Article
*/
public function setTitle($title)
{
$this->title = $title; return $this;
} /**
* Get title
*
* @return string
*/
public function getTitle()
{
return $this->title;
} /**
* Set content
*
* @param string $content
* @return Article
*/
public function setContent($content)
{
$this->content = $content; return $this;
} /**
* Get content
*
* @return string
*/
public function getContent()
{
return $this->content;
} /**
* Set author
*
* @param string $author
* @return Article
*/
public function setAuthor($author)
{
$this->author = $author; return $this;
} /**
* Get author
*
* @return string
*/
public function getAuthor()
{
return $this->author;
} /**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
}

Doctrine将所有的表字段都转换为私有的注释类属性。他还找到了表的主键,Doctrine强大在如果你的表之间有表关联模型,Doctrine生成的Entity类都能在注释映射中表现出各Entity类之间的映射关系。

Symfony2 Doctrine从现有Database生成Entity(转载自http://blog.it985.com/6809.html)的更多相关文章

  1. 记录一下idea自动生成Entity

    最近在鼓捣spring -boot ,真好用,学习到jpa. 通过生成Entity 文件,能够快速的生成数据库,并且使用 JpaRepository 的基本增删查改 方法,好用的一批. 可是随之,问题 ...

  2. IDEA 通过数据库生成entity实体类

    IDEA利用数据库生成entity类教程 1.在左上角有一个View 选项 2. 然后选择 TOOL Windows 3. 然后选择Database然后会弹出一个窗口 4.选择+号 5.选择data ...

  3. SpringBoot--Easycode、mybatisX插件生成entity,controller,service,dao,mapper IDEA版 项目提效神器

    一.介绍 Easycode是idea的一个插件,可以直接对数据的表生成entity,controller,service,dao,mapper,无需任何编码,简单而强大. MybatisX 是一款基于 ...

  4. java自动生成entity文件

    网上关于自动生成entity文件的代码很多,看了很多代码后,在先辈们的基础上再完善一些功能(指定多个表,全部表). 为了使用方便所以把两个类写在一个java文件中,所以大家可以直接拿这个java文件, ...

  5. 【Mybatis】使用Mybatis-Generator自动生成entity、dao、mapping

    使用过mybatis的应该都有用过Mybatis-Generator,本文主要介绍使用Mybatis-Generator来自动生成entity.dao.mapping文件. Mybatis-Gener ...

  6. 联想笔记本 thinkpad BIOS 超级密码 Supervisor Password 清除 破解 亲测有效 转载地址https://blog.csdn.net/ot512csdn/article/details/72571674

    联想笔记本 thinkpad BIOS 超级密码 Supervisor Password 清除 破解 亲测有效 转载地址https://blog.csdn.net/ot512csdn/article/ ...

  7. R语言中的正则表达式(转载:http://blog.csdn.net/duqi_yc/article/details/9817243)

    转载:http://blog.csdn.net/duqi_yc/article/details/9817243 目录 Table of Contents 1 正则表达式简介 2 字符数统计和字符翻译 ...

  8. (SpringBoot-Jpa)使用Idea数据库自动脚本Generate POJOS生成 Entity对象,

    因:使用SpringBoot -jpa,需要手动配置Entity 但是如果你的表中有很多属性,或者有很多表怎么办?? 每个手动写? 还是用mybatis.写mapper??? 解决:使用idea自动工 ...

  9. 从SqlServer现有数据生成Insert脚本

    步骤1,打开"Generate and Publish Objects"向导.右键点击要导出数据的数据库,选择Taks->GenerateScript 步骤2,选择要导出数据 ...

随机推荐

  1. Swift 总结使用问号(?)和感叹号(!)-备用

    在使用可选类型和可选链时,多次使用了问号(?)和感叹号(!),但是它们的含义是不同的,下面我来详细说明一下. 1. 可选类型中的问号(?) 声明这个类型是可选类型,访问这种类型的变量或常量时要使用感叹 ...

  2. cf Magic Numbers

    http://codeforces.com/contest/320/problem/A #include <cstdio> #include <cstring> using n ...

  3. 不管肉鸡,还是代理,CC识别就封杀!

    这几天的心得,汇成代码. PYTHON版,我编的. #!/usr/bin/env python # -*- coding: utf-8 -*- import os,sys,time import co ...

  4. (转)失落的C语言结构体封装艺术

    目录1. 谁该阅读这篇文章 2. 我为什么写这篇文章 3.对齐要求 4.填充 5.结构体对齐及填充 6.结构体重排序 7.难以处理的标量的情况 8.可读性和缓存局部性 9.其他封装的技术 10.工具 ...

  5. 使IE6同样支持圆角效果

    之前写到过,IE6不支持:hover效果的解决办法,其它这个跟它一样.IE6(7/8)不支持border-radius属性,所以其中的圆角效果显示不出来,可以通过引用ie-css3.htc的方法解决. ...

  6. usaco6.1-Cow XOR:trie树

    Cow XOR Adrian Vladu -- 2005 Farmer John is stuck with another problem while feeding his cows. All o ...

  7. HttpServletRequest对象方法的用法(转)

    原文地址:http://blog.csdn.net/xh16319/article/details/8450715 原文作者:ITSTAR 文章太赞,忍不住就想转..... 1. 获得客户机信息    ...

  8. [TypeScript] Avoid any type

    To avoid using "any" type is a best pratice. The reason for that is it disable the power o ...

  9. [Angular 2] WebStorm - Managing Imports

    Some tips for import libaray by using webstorm: // Alt + Enter --> Auto Import // Ctrl + Alt + o ...

  10. 使用CMD连接SQL Server

      在CMD中操作数据库,界面不美观,而且排版不整齐,但在机器上没有安装SQLSERVER的时候,也是极其方便的.   在命令行中输入 OSQL ?可以获得所有帮助信息   osql -S 数据库服务 ...