简体   繁体   English

使用Symfony2和Doctrine导入数据库

[英]Importing Database Using Symfony2 and Doctrine

I am trying to use the command 我正在尝试使用命令

doctrine:mapping:import

to import a database into my project, However I do not want the database.orm.yml files to be in a Bundle I want to have the orm and Entity files located in a Static directory in my src folder. 要将数据库导入到我的项目中,但是我不希望database.orm.yml文件位于Bundle中,我希望orm和Entity文件位于src文件夹中的Static目录中。 The command above requires me to provide a bundle name. 上面的命令要求我提供捆绑名称。 My question is, how do I import the mapping file into the folder ./src/Entity/orm folder and not into a Bundle? 我的问题是, 如何将映射文件导入到./src/Entity/orm文件夹而不是Bundle中? Is this even possible? 这有可能吗?

./src/Entity/orm        <-- Directory having the .yml files
./src/Entity/Repository <-- The repository classes
./src/Entity            <-- The Entity Classes here

I am going to answer my own question with what I found to work. 我将用发现的工作来回答自己的问题。

The following command will generate entities from the database into a specified file and adding the namespace to the classes; 以下命令将从数据库生成实体到指定文件,并将名称空间添加到类中;

php app/console doctrine:mapping:convert annotation ./src/Entity --from-database --force --namespace="Entity\\";

For a complete answer, I could have used: 对于完整的答案,我可以使用:

php app/console doctrine:mapping:convert annotation ./src/Entity --from-database --force --namespace="Entity\\";

to generate the yaml orm files. 生成yaml orm文件。 then manually added the namespace to each one followed by running: 然后手动将名称空间添加到每个名称空间,然后运行:

php app/console doctrine:generate:entities ./src/Entity/orm

after setting up the Symfony2 config.yml parameters correctly. 正确设置Symfony2 config.yml参数后。 However, that would have been a wasted effort since I can just as easily use annotations. 但是,这将是一种浪费的工作,因为我可以轻松地使用注释。

What I learned from this experience is that using yaml orm files and generating entities is a nice feature, if you are using a code first development strategy. 我从这次经验中学到的是,如果您使用的是代码优先的开发策略,则使用yaml orm文件并生成实体是一个不错的功能。 If you are just trying to generate an Entity Framework from a poorly designed or bloated database as I am, using annotations saves a headache. 如果您只是想像我这样从设计不佳或过大的数据库中生成实体框架,则使用注释可以避免麻烦。

Now what I am doing since you cannot specify repositoryClasses from the database. 现在我在做什么,因为您无法从数据库中指定repositoryClasses。 Is just adding repository classes as I develop. 在我开发时只是添加存储库类。 Not every one of the 200+ tables needs a repository class. 并非200多个表中的每个表都需要一个存储库类。

Note: This database was not designed "in house" and the common consensus is that it is terrible and needs redesign, but current time constraints are not allowing us to do that... 注意:该数据库并非“内部”设计的,并且普遍的共识是它很糟糕并且需要重新设计,但是当前的时间限制不允许我们这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM