Symfony2使用Doctrine从现有数据库导入实体

时间:2013-09-06 06:58:56

标签: php mysql symfony doctrine-orm doctrine

我是symfony的初学者。我试图按照官方文档http://symfony.com/doc/current/cookbook/doctrine/reverse_engineering.html将实体从MySQL导入Debian测试框中的symfony2项目。但我没有成功。

然后在这里搜索我在这里发现了这个Generating a single Entity from existing database using symfony2 and doctrine,但我无法使它工作,我的控制台: “要求Doctrine内省数据库并生成相应的元数据文件”

root@khs01wxl001:/var/www/organizer$ php app/console doctrine:mapping:import --force organizerscheduleBundle php
Importing mapping information from "default" entity manager
  > writing /var/www/organizer/src/organizer/scheduleBundle/Resources/config/doctrine/Dept.orm.php
  > writing /var/www/organizer/src/organizer/scheduleBundle/Resources/config/doctrine/Desg.orm.php
  > writing /var/www/organizer/src/organizer/scheduleBundle/Resources/config/doctrine/Dir.orm.php
  > writing /var/www/organizer/src/organizer/scheduleBundle/Resources/config/doctrine/Schedule.orm.php
  > writing /var/www/organizer/src/organizer/scheduleBundle/Resources/config/doctrine/Username.orm.php
  > writing /var/www/organizer/src/organizer/scheduleBundle/Resources/config/doctrine/Userrole.orm.php
root@khs01wxl001:/var/www/organizer$

到目前为止一切顺利,但现在“你可以通过执行以下两个命令来要求Doctrine构建相关的实体类。

$ php app/console doctrine:mapping:convert annotation ./src
$ php app/console doctrine:generate:entities AcmeBlogBundle

但是当我这样做时,第一个对我不起作用:

root@khs01wxl001:/var/www/organizer$ php app/console doctrine:mapping:convert annotation ./src/organizer/scheduleBundle/Resources/config/doctrine/
No Metadata Classes to process.
root@khs01wxl001:/var/www/organizer$

有什么建议吗?

2 个答案:

答案 0 :(得分:6)

尝试:

# write the structure to annotation file (I prefear to use YML instead annotation but should be the same) 
$ php app/console doctrine:mapping:convert annotation ./src/organizer/scheduleBundle/Resources/config/doctrine/metadata/orm --from-database --force

# Import the structure
$ php app/console doctrine:mapping:import organizerscheduleBundle annotation

# Generate Entities file class
$ php app/console doctrine:generate:entities organizerscheduleBundle 

此处有更多文档:

http://docs.doctrine-project.org/en/2.0.x/reference/tools.html#reverse-engineering

并非所有人都会被导入(文件说70-80%,但在我看来更少)。

PS。最好的方法是使用管理器作为公司名称调用BundizerScheduleBundle(看大写字母),因此在开发人员/公司名称文件夹中将包含所有捆绑包。

答案 1 :(得分:1)

如果您正在使用symfony 3并想使用注释,我使用了这个:

php bin/console doctrine:mapping:import --force AcmeBlogBundle annotation

这可以生成您所需的一切,而无需使用doctrine:mapping:convert