从数据库生成实体 [英] Generating entities from database

查看:113
本文介绍了从数据库生成实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从现有的空数据库中生成symfony2中的实体(相当大,从头开始创建实体是非常痛苦的)。不幸的是,我遇到了很大的问题。

I'm trying to generate entities in symfony2 from an existing empty database (quite large and it would be real pain to create entities from scratch). Sadly, I've encountered rather big problem.

当我尝试调用以下命令(在Windows上,如果更改任何内容):

When I try to call the following command (on windows if it changes anything):

php app/console doctrine:mapping:convert --force --from-database annotation ./src/GOutside/GOBundle/Resources/config/doctrine

我收到以下消息:


没有要处理的元数据类。

No Metadata Classes to process.

发布之前我有:


  • 验证我的配置是否正常(我可以连接到数据库)

  • 我的包被创建,上面指定的路径是有效的

  • 当我尝试转换为xml / yml时,当我尝试生成注释映射时,同样的事情发生。

Symfony版本是2.4.4,php版本是5.5.3。

Symfony version is 2.4.4, php version is 5.5.3.

感谢提前的帮助:)

xiidea回复后编辑

Edit after xiidea reply

当我尝试使用

php app/console doctrine:mapping:import --force GOBundle xml

按照@xiidea的建议,我收到以下消息:

as suggested by @xiidea, I got following message:


数据库没有任何映射信息。

Database does not have any mapping information.


推荐答案

您的案例在symfony网站上有详细记录,标题为如何从现有数据库生成实体

Your case is well documented in symfony website titled "How to generate Entities from an Existing Database"

正如文档所述:


从现有的
数据库中建立实体类的第一步是询问Doctrine内省数据库并生成
相应的元数据文件。元数据文件描述了基于表字段生成的实体
类。

The first step towards building entity classes from an existing database is to ask Doctrine to introspect the database and generate the corresponding metadata files. Metadata files describe the entity class to generate based on table fields.

使用以下命令(假设您的软件包的短名称为 GOutsideGobundle

Using following command (assuming your bundle's short name is GOutsideGOBundle)


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

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

然后你需要调用。


php app / console doctrine:generate:entities GOutsideGobundle

php app/console doctrine:generate:entities GOutsideGOBundle

如果您需要使用注释映射生成实体类,则必须在 doctrine:generate之前执行以下命令:实体

If you need to generate entity classes with annotation mappings, then you have to execute the following command before doctrine:generate:entities


php app / console doctrine:mapping:convert annotation ./src

php app/console doctrine:mapping:convert annotation ./src

路径应该只是 ./ src 而不是 ./ src / GOutside / GOBundle / Resources / config / doctrine

Path should be only ./src instead of ./src/GOutside/GOBundle/Resources/config/doctrine

如果您的配置中的一切都正确,错误 数据库没有任何映射信息。不太可能!我不知道这个错误。但是,根据您的数据库表模式,有一些问题可能会阻止您创建映射信息。

If everything is correct in your configuration then getting the error Database does not have any mapping information. is unlikely!! I am not sure about this error. But as per your database table schema, there are some issue which will prevent you from creating mapping information.


  1. 您的表格包含特殊类型哪个教义无法处理。

  2. 您的数据库表有多个表,没有任何主键。 Doctrine不支持没有主键的表的反向工程

  1. Your table contain special type point which doctrine can't handle.
  2. You database table has multiple table without any primary key. Doctrine does not support reverse engineering from tables that don't have a primary key

解决问题( 1)你可以添加一个。例如,将点类型映射为可以写入的字符串:

To solve problem (1) you can add a custom mapping in your doctrine config section. For example to map the point type as string you can write:

doctrine:
    dbal:
        //Other connection parameters
        mapping_types:
            point: string

您需要为这些表定义主键,那些可能是新字段或可能是复合主键。

for the second problem you need to define primary key for those tables, those could be new field or could be composite primary key.

这篇关于从数据库生成实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆