php 使用 symfony2 和学说从现有数据库生成单个实体

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10371600/
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-24 22:02:00  来源:igfitidea点击:

Generating a single Entity from existing database using symfony2 and doctrine

phpdatabasesymfonydoctrine

提问by Sethunath

Is it possible to generate a single entity from database using the Symfony2 console tool?

是否可以使用 Symfony2 控制台工具从数据库生成单个实体?

In the middle of coding I had to add a table and there are modifications made to the existing entity classes. So I don't want all my entities regenerated.

在编码过程中,我不得不添加一个表格,并对现有的实体类进行了修改。所以我不希望我所有的实体都重新生成。

Any suggestions will be appreciated!

任何建议将不胜感激!

回答by Snroki

I had the same problem, you've to do this way:

我有同样的问题,你必须这样做:

php app/console doctrine:mapping:convert metadata_format \
    ./src/App/MyBundle/Resources/config/doctrine \
    --from-database \
    --filter="Yourtablename"

Then

然后

php app/console doctrine:mapping:import AppMyBundle \
    metadata_format --filter="Yourtablename"

Where metadata_formatis the file ending you want to generate (e.g. xml, yml, annotation)

metadata_format你要生成的文件结尾在哪里(比如xml、yml、annotation)

And finally

最后

php app/console doctrine:generate:entities AppMyBundle --no-backup

Like this doctrine will load only the entity you need. Just be carefull on the filter you must use the CamelCase !

像这个学说只会加载你需要的实体。请注意过滤器,您必须使用 CamelCase !

Hope this will help you

希望能帮到你

回答by jpb

For the third command, doctrine kept regenerating all of the Entity files. By adding the entity name after the bundle, it only generated the entity I was interested in.

对于第三个命令,学说不断重新生成所有实体文件。通过在 bundle 后面添加实体名称,它只生成了我感兴趣的实体。

php app/console doctrine:generate:entities AppMyBundle:Yourtablename --no-backup

回答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 个命令:

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

(NOTE:if your database name is my_meetingYou will need to change it to MyMeetingin filter="MyMeeting"for doctrine to find your table name. This is because doctrine will always strip underscores and add Camel-case to your table name. If not, you will get this error: "Database does not have any mapping information".)

注意:如果您的数据库名称是my_meeting您将需要将其更改为MyMeetinginfilter="MyMeeting"来查找您的表名。这是因为 Dot 总是去除下划线并将 Camel-case 添加到您的表名中。如果不是,您将收到此错误:“数据库没有任何映射信息”。)

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

(NOTE:making sure you have namespace AppBundle\Entity;as below in your Meeting.phpclass file like this:

注意:确保您namespace AppBundle\Entity;Meeting.php类文件中有如下内容:

<?php
/**
* Created by PhpStorm.
* User:
* Date: 03-Sep-2015
* Time: 3:23 PM
*/
namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

If not add it in.)

如果没有添加它。)

where:

在哪里:

  • AppBundle is exactly your "AppBundle" in Symfony 2.7
  • Meeting is the target table (Camel-Case sensitive)
  • AppBundle 就是你在 Symfony 2.7 中的“AppBundle”
  • Meeting 是目标表(Camel-Case 敏感)

TO BE SURE, check this directory:

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

src\AppBundle/Resources/config/doctrine/Meeting.orm.xml

src\AppBundle/Resources/config/doctrine/Meeting.orm.xml

AND MAKING SURE you only have .xml files for the table you want to create entity class files and no others. Then run this below command to generate get and set methods for your entity class that you created previously

并确保您只有要创建实体类文件的表的 .xml 文件,没有其他文件。然后运行下面的命令为您之前创建的实体类生成 get 和 set 方法

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

$ php 应用程序/控制台学说:生成:实体 AppBundle:会议 --no-backup

NOTE2: As the last step you must delete the xml doctrine orm db file in for example src\AppBundle/Resources/config/doctrine/VisitorData.orm.xml

注意2:作为最后一步,您必须删除例如 xml 学说 orm db 文件 src\AppBundle/Resources/config/doctrine/VisitorData.orm.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 Carlton

@fyrye's comment that is currently hidden deserves the credit, wanted to add this so it's not missed by others. This is the approach:

@fyrye 目前隐藏的评论值得称赞,想添加此内容以免其他人错过。这是方法:

/** @var \Doctrine\DBAL\Connection $connection */
$config = $connection->getConfiguration();

// for excluding an specific table
$config->setFilterSchemaAssetsExpression('/^(table_to_reverse_engineer_1|table_to_reverse_engineer_2).*$/');

source: https://coderwall.com/p/jofhdw/doctrine-tell-which-tables-to-work-with

来源:https: //coderwall.com/p/jofhdw/doctrine-tell-which-tables-to-work-with

I was having issues when running the following command because of large number of badly defined legacy tables

由于大量定义错误的旧表,我在运行以下命令时遇到问题

php ./vendor/bin/doctrine orm:convert-mapping --force --from-database annotation ./src/UI/Entity/

It turns out that the --filter flag only filters AFTER it has read meta data from all of your tables which, if they don't have primary keys or have some other issue, will cause the command to fail

事实证明 --filter 标志仅在它从所有表中读取元数据后才进行过滤,如果它们没有主键或有其他问题,将导致命令失败

回答by Brett Thomas

None of the answers were quite right for me using Symfony 3. I ended up doing:

对于使用Symfony 3 的我来说,没有一个答案是完全正确的。我最终做了:

php bin/console doctrine:mapping:import --force MyBundle xml --filter="MyTable"

php bin/console doctrine:mapping:convert annotation ./src --filter="MyTable"

php bin/console doctrine:generate:entities MyBundle:MyTable --path ./src

回答by Ivo Necchi

Works great with Symfony 3 too.

也适用于 Symfony 3。

If you are getting "No Metadata Classes to process." message try convert your tablename to Doctrine camel casing in the filter parameter.

如果您收到“没有要处理的元数据类”。消息尝试在过滤器参数中将您的表名转换为 Doctrine 驼峰式外壳。

"my_table_name" needs to be write as "MyTableName".

“my_table_name”需要写成“MyTableName”。

回答by peej

I would have left this as a comment to the accepted answer but I'm a newbie.

我会将其作为对已接受答案的评论,但我是新手。

For those like me who had trouble with the --filter switch mapping multiple tables with coincident strings in names, one can use a pattern.

对于像我这样在 --filter 开关映射名称中具有重合字符串的多个表时遇到问题的人,可以使用一种模式。

Example table names:

示例表名:

Vendor VendorContact

供应商供应商联系

php app/console doctrine:mapping:convert metadata_format \
    ./src/App/MyBundle/Resources/config/doctrine \
    --from-database \
    --filter="Vendor"

That command will convert both tables rather than just Vendor. If you want just Vendor and not VendorContact, use a pattern in --filter:

该命令将转换两个表,而不仅仅是 Vendor。如果您只需要 Vendor 而不是 VendorContact,请在 --filter 中使用模式:

php app/console doctrine:mapping:convert metadata_format \
    ./src/App/MyBundle/Resources/config/doctrine \
    --from-database \
    --filter="\bVendor\b"

Hope that helps someone!

希望对某人有所帮助!

回答by user13424261

--filter works with entity name, not table name ! php bin/console doctrine:mapping:import "App\Entity" annotation --path=config/doctrine --filter="YourEntity"

--filter 使用实体名称,而不是表名称!php bin/console algorithm:mapping:import "App\Entity" annotation --path=config/doctrine --filter="YourEntity"

回答by max4ever

for Symfony 3

对于Symfony 3

To generate the entities for a new "Group" table

为新的“组”表生成实体

php bin/console doctrine:mapping:import "App\Entity" annotation --path=src/AppBundle/Entity --filter Group

enter image description here

在此处输入图片说明

as written in the symfony 3 documentation

symfony 3 文档中所写

回答by Nicolas

I had exactly the same issue with Symfony 2.4 and MySQL.

我在 Symfony 2.4 和 MySQL 上遇到了完全相同的问题。

None of the workarounds posted above worked for me.

上面发布的所有解决方法都不适合我。

I ended up creating a new database with the tables I want to extract (this can be easy to do because MySQL provides the creation script).

我最终用我想要提取的表创建了一个新数据库(这很容易做到,因为 MySQL 提供了创建脚本)。

Then changed the connection to that new database and executed the entity extraction command from there.

然后更改与该新数据库的连接并从那里执行实体提取命令。

Seems to be a bit radical, but I will not create the entities by hand.

似乎有点激进,但我不会手工创建实体。

Hope that helps

希望有帮助