I need to load database and entity configuration in Symfony2 from tables in the database in runtime. By default, Symfony database config is stored in config.yml. Table names for the entity are defined in @ORM annotations. 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. I want to set the default database config in config.yml. This database will store three tables:
- Contains connections to databases (database_connection_id, host, port, user, password, dbname)
- Contains relation (entity_name, table_name)
- Contains relation (table_name, database_connection_id)
I need to load this configuration dynamically in web request before making requests to entities using EntityManager or EntityRepository. In other words, I want to process this configuration, set table_name and connection for each entity, before processing web requests in Controller. 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. The table name for the entity can be set using Class Metadata, but I am not sure it is the right decision.
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. But this way looks ugly. Furthermore, background tasks can want to work with other tables for an entity than web requests. 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.
Can this implement using standard symfony flexible components?