php 如何从symfony2中的现有表生成实体?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13394855/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:19:46  来源:igfitidea点击:

How to generate entity from existing table in symfony2?

phpsymfony

提问by Alastor

I have table "my_table" with some fields. I want generate Entity in MyBundle used "my_table". But I don't want recreate all entities in MyBundle. How can I do this?

我有一些字段的表“my_table”。我想在 MyBundle 中使用“my_table”生成实体。但我不想在 MyBundle 中重新创建所有实体。我怎样才能做到这一点?

回答by Ahmed Siouani

Here is the way you can do it,

这是你可以做到的方法,

First step, ask Doctrine to introspect the database and generate the corresponding xml or yml metadata files.

第一步,让 Doctrine 内省数据库并生成相应的 xml 或 yml 元数据文件。

php app/console doctrine:mapping:convert [xml|yml] Path/To/MyBundle/Resources/config/doctrine/metadata/orm --from-database --force --filter=MyTable

Second step, ask Doctrine to import the schema and build related entity classes by executing the following two commands.

第二步,通过执行以下两个命令,让 Doctrine 导入 schema 并构建相关的实体类。

php app/console doctrine:mapping:import MyBundle [xml|yml|annotation] --filter=MyTable

php app/console doctrine:generate:entities Path\To\MyBundle\EntityFolder\MyTable

Take a look at the How to generate Entities from an Existing Databasesection of the documentation

查看文档的如何从现有数据库生成实体部分

回答by Dung

Simple Working Solution for Symfony 2.7 option annotation and for [/xml/yml] see http://symfony.com/doc/current/cookbook/doctrine/reverse_engineering.html

Symfony 2.7 选项注释和 [/xml/yml] 的简单工作解决方案见http://symfony.com/doc/current/cookbook/doctrine/reverse_engineering.html

do 3 commands in 3 steps:

分 3 个步骤执行 3 个命令:

Command #1:

命令#1:

$ php app/console doctrine:mapping:import --force AppBundle xml --filter="Meeting"

Output:

输出:

writing C:\xampp\htdocs\localxyz\src\AppBundle/Resources/config/doctrine/Meeting.orm.xml

写 C:\xampp\htdocs\localxyz\src\AppBundle/Resources/config/doctrine/Meeting.orm.xml


Command #2:


命令#2:

$ php app/console doctrine:mapping:convert annotation ./src/AppBundle/Entity --from-database --filter="Meeting"

Output:

输出:

Processing entity "Meeting"

Exporting "annotation" mapping information to "C:\xampp\htdocs\localxyz\src\Entity"

处理实体“会议”

将“注解”映射信息导出到“C:\xampp\htdocs\localxyz\src\Entity”


Command #3:


命令#3:

$ php app/console doctrine:generate:entities AppBundle:Meeting --no-backup

Output:

输出:

Generating entity "AppBundle\Entity\Meeting" generating AppBundle\Entity\Meeting

生成实体“AppBundle\Entity\Meeting”生成AppBundle\Entity\Meeting

where:

在哪里:

AppBundle is exactly your "AppBundle" in 2.7 symfony Meeting is the target table (case sensitive)

AppBundle 正是您在 2.7 symfony 中的“AppBundle”会议是目标表(区分大小写)

TO BE SURE, check this directory:

为了确保,请检查此目录:

C:\xampp\htdocs\myproj\src\AppBundle/Resources/config/doctrine/Meeting.orm.xml

C:\xampp\htdocs\myproj\src\AppBundle/Resources/config/doctrine/Meeting.orm.xml

C:\xampp\htdocs\myproj\src\AppBundle/Resources/config/doctrine/MeetingOriginal.orm.xml

C:\xampp\htdocs\myproj\src\AppBundle/Resources/config/doctrine/MeetingOriginal.orm.xml

AND MAKING SURE you only have .xml files for the table you want to create entity class files and no others.

并确保您只有要创建实体类文件的表的 .xml 文件,没有其他文件。

It works very well for me.

它对我来说非常有效。

For explanation please read: http://symfony.com/doc/current/cookbook/doctrine/reverse_engineering.html

解释请阅读:http: //symfony.com/doc/current/cookbook/doctrine/reverse_engineering.html

回答by Ondrej Slinták

php app/console doctrine:mapping:import "MyCustomBundle" xml --filter=MyMatchedEntity

回答by Azhar Khattak

Although this is an old post but if someone gets following error,

虽然这是一个旧帖子,但如果有人出现以下错误,

Database does not have any mapping information.

Database does not have any mapping information.

Check

查看

If your table name is blog_postthen in filter option use BlogPostand not blog_post

如果您的表名blog_post在过滤器选项中,则使用BlogPost而不是blog_post

Reference: https://stackoverflow.com/a/27019561/6504104

参考:https: //stackoverflow.com/a/27019561/6504104

though this is covered by answers above but I missed it and was getting this error

虽然上面的答案涵盖了这一点,但我错过了它并收到此错误

So I wanted to make this explicit

所以我想明确说明这一点

Also in symfony >= 3.4 its' php bin/consolee.g.

同样在 symfony >= 3.4 中,php bin/console例如

php bin/console doctrine:mapping:import --force AppBundle xml --filter="BlogPost"

and then

进而

php bin/console doctrine:mapping:convert annotation ./src/AppBundle/Entity --from-database --filter="BlogPost"

Thanks...

谢谢...