简体   繁体   English

从实体生成Zend Framework 2和Doctrine 2数据库

[英]Zend framework 2 and Doctrine 2 database generation from entities

Is it possible to generate the database in a modular fashion ? 是否可以以模块化方式生成数据库? making relations between entities available depending on which modules are loaded ? 使实体之间的关系取决于要加载的模块?

Example Setup 设定范例

  • Zend framework 2 Zend框架2
  • DoctrineORMModule DoctrineORMModule
  • Doctrine 教义

Modules 模块

  • User 用户
  • Group

Each module has corresponding entities which they manage. 每个模块都有它们管理的相应实体。 using annotations for database creation and proxy creation. 使用注释进行数据库创建和代理创建。

relation between user and group 1 -> many or 1 -> 1 用户和组之间的关系1->多或1-> 1

Application 1 应用1

uses module user and group 使用模块用户和组

Application 2 应用2

uses module user but not group. 使用模块用户但不使用组。

is it possible to describe a relation between user and group using doctrine annotations that will be removed if a entity is not present ? 是否可以使用学说注释来描述用户和组之间的关系,如果实体不存在,该注释将被删除?

remove or add the relation when ./vendor/bin/doctrine-module orm:schema-tool:create or ./vendor/bin/doctrine-module orm:schema-tool:update is called 在调用./vendor/bin/doctrine-module orm:schema-tool:create或./vendor/bin/doctrine-module orm:schema-tool:update时删除或添加关系

No, you shouldn't remove the annotation if a module is not loaded. 不,如果未加载模块,则不应删除注释。 You rather should look at things differently. 您宁愿以不同的方式看待事物。

Seen from your setup of application 1 and 2, the user will be installed and group is depending on user. 从应用程序1和2的设置中可以看到,将安装用户,并且组取决于用户。 Note that for application 1, user is not depending on group. 请注意,对于应用程序1,用户取决于组。 This means the relation is unidirectional : a group is aware of its users but the user is not aware of its group. 这意味着该关系是单向的 :组知道其用户,但用户不知道其组。 As you said, the relation is also many:many. 正如您所说,这种关系也很多:很多。 And surprisingly, Doctrine has an association mapping called many-to-many, unidirectional . 令人惊讶的是,Doctrine具有一个称为多对多单向的关联映射。

The only thing is that you can load a user's group only via the Group . 唯一的事情是您只能通过Group加载用户的组。 You cannot do things like this: 您不能做这样的事情:

$user   = $repository->find(1);
$groups = $user->getGroups();

The user is not aware of the groups, so it cannot load them. 用户不知道这些组,因此无法加载它们。 Instead, ask the group repository for the groups: 而是向群组存储库询问群组:

$user   = $userRepository->find(1);
$groups = $groupRepository->findAllByUser($user);

This will ask the group repository to find all groups where the given user is related to. 这将要求组存储库找到与给定用户相关的所有组。

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

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