如何使用doctrine和symfony2从数据库视图生成实体

时间:2022-04-17 06:46:33

I am trying to generate entities from database using standard console commands as described in Symfony2 documentation here: http://symfony.com/doc/current/cookbook/doctrine/reverse_engineering.html.

我正在尝试使用标准控制台命令从数据库生成实体,如Symfony2文档中所述:http://symfony.com/doc/current/cookbook/doctrine/reverse_engineering.html。

php app/console doctrine:mapping:convert --from-database --force yml "src/My/HomeBundle/Resources/config/doctrine/metadata/orm"
php app/console doctrine:mapping:import MyHomeBundle yml
php app/console doctrine:generate:entities MyHomeBundle

After this, all tables are generated correctly. The problem is that this won't generate entities for database views. When I add yml files myself into src/My/HomeBundle/Resources/config/doctrine/metadata/orm for example:

在此之后,所有表都正确生成。问题是这不会为数据库视图生成实体。当我自己将yml文件添加到src / My / HomeBundle / Resources / config / doctrine / metadata / orm中时,例如:

UserInGroup:
  type: entity
  table: user_in_group_view
  fields:
    id:
      id: true
      type: integer
      unsigned: false
      nullable: false
      generator:
        strategy: IDENTITY
    userId:
      type: integer
      unsigned: false
      nullable: false
      column: user_id
    userGroupId:
      type: integer
      unsigned: false
      nullable: false
      column: user_group_id
  lifecycleCallbacks: {  }

I get this exception when running php app/console doctrine:generate:entities MyHomeBundle:

我在运行php app / console doctrine时遇到此异常:generate:entities MyHomeBundle:

Notice: Undefined index: My\HomeBundle\Entity\UserInGroup in C:\Users\ThisIsMe\Projects\SymfonyTestProject\vendor\doctrine\lib\Doctrine\ORM\Mapping\Driver\AbstractFileDriver.php line 121

Similar question was posted here: How to set up entity (doctrine) for database view in Symfony 2

类似的问题发布在这里:如何在Symfony 2中为数据库视图设置实体(学说)

I know I can create Entity class, but I was hoping that I could get this generated so if I change my view, I could just regenerate entity classes. Any suggestions?

我知道我可以创建Entity类,但是我希望我能够生成这样的内容,所以如果我改变了我的视图,我就可以重新生成实体类。有什么建议么?

3 个解决方案

#1


2  

Now you create your orm files only. You need to follow 2 more steps. I will give you the complete steps from begining.

现在只创建您的orm文件。您需要再执行2个步骤。我会从开始给你完整的步骤。

Before doing this delete all yml files in your orm directory that you had created early.

在此之前删除您早期创建的orm目录中的所有yml文件。

I hope MyHomeBundle is your bundle name

我希望MyHomeBundle是你的包名

1).php app/console doctrine:mapping:convert yml ./src/My/HomeBundle/Resources/config/doctrine --from-database --force 

  Symfony2 generate entity from Database

 2).php app/console doctrine:mapping:import MyHomeBundle yml

 3).php app/console doctrine:generate:entities MyHomeBundle

Hope this helps you.

希望这对你有所帮助。

#2


0  

Got the same issue, i use xml instead of yml but must be the same.

有同样的问题,我使用xml而不是yml,但必须是相同的。

Check in your orm entity if the name include the correct route, exemple:

如果名称包含正确的路由,请检查您的orm实体,例如:

<entity name="Myapp\MyrBundle\Entity\MyEntity" table="myentity">

Because when i generate my orm from database the name was like that:

因为当我从数据库生成我的orm时,名称是这样的:

<entity name="MyEntity" table="myentity">

So doctrine didn't understand the right path.

所以学说不明白正确的道路。

Hope i'm clear and this will help you!

希望我很清楚,这会对你有所帮助!

#3


-1  

As you can see here: http://symfony.com/doc/current/cookbook/doctrine/reverse_engineering.html the reverse engineering process from db to entity is not fully implemented yet:

正如您在此处所见:http://symfony.com/doc/current/cookbook/doctrine/reverse_engineering.html从db到实体的逆向工程过程尚未完全实现:

"As the Doctrine tools documentation says, reverse engineering is a one-time process to get started on a project. Doctrine is able to convert approximately 70-80% of the necessary mapping information based on fields, indexes and foreign key constraints. Doctrine can't discover inverse associations, inheritance types, entities with foreign keys as primary keys or semantical operations on associations such as cascade or lifecycle events. Some additional work on the generated entities will be necessary afterwards to design each to fit your domain model specificities."

“正如Doctrine工具文档所说,逆向工程是一个一次性开始项目的过程.Protrine能够根据字段,索引和外键约束转换大约70-80%的必要映射信息。发现反向关联,继承类型,具有外键的实体作为主键或对诸如级联或生命周期事件之类的关联的语义操作。之后需要对生成的实体进行一些额外的工作以设计每个以适合您的域模型特性。

#1


2  

Now you create your orm files only. You need to follow 2 more steps. I will give you the complete steps from begining.

现在只创建您的orm文件。您需要再执行2个步骤。我会从开始给你完整的步骤。

Before doing this delete all yml files in your orm directory that you had created early.

在此之前删除您早期创建的orm目录中的所有yml文件。

I hope MyHomeBundle is your bundle name

我希望MyHomeBundle是你的包名

1).php app/console doctrine:mapping:convert yml ./src/My/HomeBundle/Resources/config/doctrine --from-database --force 

  Symfony2 generate entity from Database

 2).php app/console doctrine:mapping:import MyHomeBundle yml

 3).php app/console doctrine:generate:entities MyHomeBundle

Hope this helps you.

希望这对你有所帮助。

#2


0  

Got the same issue, i use xml instead of yml but must be the same.

有同样的问题,我使用xml而不是yml,但必须是相同的。

Check in your orm entity if the name include the correct route, exemple:

如果名称包含正确的路由,请检查您的orm实体,例如:

<entity name="Myapp\MyrBundle\Entity\MyEntity" table="myentity">

Because when i generate my orm from database the name was like that:

因为当我从数据库生成我的orm时,名称是这样的:

<entity name="MyEntity" table="myentity">

So doctrine didn't understand the right path.

所以学说不明白正确的道路。

Hope i'm clear and this will help you!

希望我很清楚,这会对你有所帮助!

#3


-1  

As you can see here: http://symfony.com/doc/current/cookbook/doctrine/reverse_engineering.html the reverse engineering process from db to entity is not fully implemented yet:

正如您在此处所见:http://symfony.com/doc/current/cookbook/doctrine/reverse_engineering.html从db到实体的逆向工程过程尚未完全实现:

"As the Doctrine tools documentation says, reverse engineering is a one-time process to get started on a project. Doctrine is able to convert approximately 70-80% of the necessary mapping information based on fields, indexes and foreign key constraints. Doctrine can't discover inverse associations, inheritance types, entities with foreign keys as primary keys or semantical operations on associations such as cascade or lifecycle events. Some additional work on the generated entities will be necessary afterwards to design each to fit your domain model specificities."

“正如Doctrine工具文档所说,逆向工程是一个一次性开始项目的过程.Protrine能够根据字段,索引和外键约束转换大约70-80%的必要映射信息。发现反向关联,继承类型,具有外键的实体作为主键或对诸如级联或生命周期事件之类的关联的语义操作。之后需要对生成的实体进行一些额外的工作以设计每个以适合您的域模型特性。