Symfony2:有条件地创建实体 table

Symfony2: Creating entity table conditionally

我有一个包,其中定义了实体。我希望能够以这种方式配置这个包,这个实体将或不相关。因此,如果 bundle 配置正确,则不应使用 app/console doctrine:schema:update 等创建实体 table,或者应该是 - 它应该取决于配置。

如何有条件地 "disable" 实体使其 table 不会被 app/console doctrine:schema:update 创建?

我认为,虽然也许您可以找到一种方法,但您正在使自己复杂化。如果后端包是独立的,那么安装它总是可选的,因此它是创建或不创建实体。

您可以在 Sonata bundle 中找到一个示例,您可以根据需要管理用户,但是如果您使用的是 FOSUserBundle,您可以选择安装 SonataUserBundle,然后告诉 fos_user 配置新 class 属于 Sonata 用户,因此由于 class 继承,新实体将保留许多新属性,并且用户的所有 CRUD 操作都已在奏鸣曲视图中配置。 SonataUser 也有自己的用户实体,可以独立使用。

我知道这不是您的要求,但您可能只需要设法遵循这样的模型。

您的方案要求您禁用 auto_mapping,但它似乎默认设置为 falsehttp://symfony.com/doc/current/reference/configuration/doctrine.html

接下来要做的是确保您的包的 build 功能有条件地添加想要的 DoctrineOrmMappingPass as also is explained here:

尽你所能see in the source, build only is executed once the cache is empty so this is the place where you can do this. You can also take a look at how to add compiler passes