Symfony2 Doctrine从现有Database生成Entity(转载自http://blog.it985.com/6809.html)

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

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

我们来做一个例子:

1.创建两张表:section,article

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类之间的映射关系。

posted @ 2016-03-17 14:40  jyLi  阅读(1047)  评论(0编辑  收藏  举报