简体   繁体   English

从 Symnfy 中的数据库加载实体配置

[英]Load entity configuration from database in Symonfy

I need to load database and entity configuration in Symfony2 from tables in the database in runtime.我需要在运行时从数据库中的表加载 Symfony2 中的数据库和实体配置。 By default, Symfony database config is stored in config.yml.默认情况下,Symfony 数据库配置存储在 config.yml 中。 Table names for the entity are defined in @ORM annotations.实体的表名在@ORM 注释中定义。 But some of my entities can be stored dynamically in any database with any table name and in advance, it is not defined (except table schema), so I can't store database configuration in config.但是我的一些实体可以使用任何表名动态存储在任何数据库中,并且事先没有定义(表模式除外),因此我无法在 config.xml 中存储数据库配置。 I want to set the default database config in config.yml.我想在 config.yml 中设置默认数据库配置。 This database will store three tables:该数据库将存储三个表:

  1. Contains connections to databases (database_connection_id, host, port, user, password, dbname)包含到数据库的连接(database_connection_id、主机、端口、用户、密码、dbname)
  2. Contains relation (entity_name, table_name)包含关系(entity_name, table_name)
  3. Contains relation (table_name, database_connection_id)包含关系(table_name,database_connection_id)

I need to load this configuration dynamically in web request before making requests to entities using EntityManager or EntityRepository.在使用 EntityManager 或 EntityRepository 向实体发出请求之前,我需要在 Web 请求中动态加载此配置。 In other words, I want to process this configuration, set table_name and connection for each entity, before processing web requests in Controller.换句话说,我想在Controller中处理Web请求之前处理这个配置,为每个实体设置table_name和connection。 And then work with entities transparent as usually.然后像往常一样使用透明的实体。

As I understand I need to implement something like Symfony ConfigLoader, but there is no database connection while processing configuration.据我了解,我需要实现类似 Symfony ConfigLoader 的东西,但是在处理配置时没有数据库连接。 The table name for the entity can be set using Class Metadata, but I am not sure it is the right decision.可以使用 Class Metadata 设置实体的表名,但我不确定这是正确的决定。

A possible way is to generate symfony config.yml for connections and src/*Bundle/Resources/config/doctrine/*.orm.yml for table names (instead @ORM annotations) from database each web request (or once when database config is changed in default database), clear symfony database cache and then load kernel of Symfony.一种可能的方法是为连接生成 symfony config.yml 和 src/*Bundle/Resources/config/doctrine/*.orm.yml 为表名(而不是 @ORM 注释)从数据库每个 Web 请求(或一次当数据库配置是在默认数据库中更改),清除 symfony 数据库缓存,然后加载 Symfony 内核。 But this way looks ugly.但是这种方式看起来很丑。 Furthermore, background tasks can want to work with other tables for an entity than web requests.此外,后台任务可能希望使用实体的其他表而不是 Web 请求。 Each entity can have more than one table, the background task can generate a new table, web requests at this time uses the previous version of the table.每个实体可以有多个表,后台任务可以生成一个新表,此时web请求使用的是以前版本的表。

Can this implement using standard symfony flexible components?这可以使用标准的 symfony 灵活组件来实现吗?

I do not think that this is a viable idea.我不认为这是一个可行的想法。 It would come at a great performance cost since symfony caches config files for production and you want to create your configuration at runtime each request.由于 symfony 缓存用于生产的配置文件,并且您希望在每个请求运行时创建配置,因此这会带来很大的性能成本。

If you want to do something similar, create a console command that creates your mappings and config files and let symfony clear and recreate its cache with php app/console clear:cache --env=prod or something similar.如果你想做类似的事情,创建一个控制台命令来创建你的映射和配置文件,让 symfony 清除并使用php app/console clear:cache --env=prod或类似的东西重新创建它的缓存。

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

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