Load entity configuration from database in Symonfy - php

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?

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.
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.

Related

How can use multiples database in symfony?

I have a system with many clients, but for each client we have one database "all database and code are the same", but I can set just one database in .env file, how can I set for each client the correct database.
So, on this case I need to create for each client one folder with the same files in each folder, also i did this on my public/index.php
https://onlinephp.io/c/b61dc
but this one I set differents .env for each client with their own databases, so for each client I'll have one .env
There is the best way to do this?
I know that could I use one database for all clients differentiating by customer_id in each table, but on this for now i'll need to change a lot on the system, i'll do this in a v2
Symfony v5.4

Performnce overhead Session Vs GUID from Meta table approcah. Yii app instances using dynamic config files created at runtime

In my yii application I am creating the app by passing config files and db parameters dynamically at runtime based on the users input. My app can have thousands of users and thus thousands of instances with separate db connections of same code base. To achieve that I have separate config files for each user.
My question:
Should I use session variable or a GUID value from meta table to map the config file path (currently using session) ?
Is there any possible limitation imposed by Yii, PHP or Apache on numbers of instances of same yii app created this way concurrently? What re performance overhead

Multiple database config file with Laravel

I've developed a Web application with Laravel and everything is ok.
This should be a multi-tenant application, so I would like to share the same code but to use a different database for each tenant (I decided for this architecture as according to me it is too complex to share database schema or records among tenants).
Every tenant is accessing the application with its own third level domain (tenant1.xxxx.com, tenant2.xxxx.com, etc)
I would like then to create n. databases (tenant1, tenant2, etc) and to create n. database config file in Laravel (database.tenant1.php, database.tenant2.php, etc)
The problem now is that I cannot find an elegant way to alter the database config file loading system in Laravel.
I should select the config file, based on the host name used by the customer.
Any help would be appreciated.
Thank you,
Michele
in your config folder, there is a 'database.php' file. in that file you can see an array called 'connection' .
'connection' array manages different configuration for multiple database connections.
you can define your configuration for each of tenants in 'connection' array and based on the scenario you are in it, you can choose the appropriate connection to handle your query with a syntax like this:
DB::connection('tenant1')->select('where...');
or
DB::connection('tenant2')->select('where...');

Symfony2 load settings from database

I have a db table (doctrine entity) that I use to store some editable settings for my app, like page title, maintenance mode (on/off), and some other things..
I can load the settings normally using the entity manager and repositories, but I think that's not the best solution...
My questions are:
- can I load the settings only once at some kernel event and then access them the same way I access any other setting saved in yml config files..
how can I cache the database settings, so I would only do one DB query, and then in future page requests, it would use the cached values, instead of doing a DB query for each page request? (of course, everytime I change something in the settings, I would need to purge that cache, so the new settings could take effect)
LiipDoctrineCacheBundle provides a service wrapper around Doctrine's common Cache (documentation) that allows you to use several cache drivers like filesystem, apc, memcache, ...
I would recommend loading your generic container-parameters/settings (like maintainance mode,...) from database in a bundle-extension or a compiler-pass.
route-specific settings (like page title, ...) could be loaded in a kernel event listener. You can find a list of kernel events here.
update/invalidate their cache using a doctrine postUpdate/postPersist/postRemove listener.

PHP/mysql application creation process

I am trying to figure out how one would start the setup of a small CMS.
I have the groundwork built, but the step of creating the database tables in mysql, should this all be done at once in a install.php file? Is there a preferred method for creating many tables at once, even if I don't need to insert data into them at this time?
You can
Import the schema file to your database prior to deploying the application
You can have a script that creates the schema
You can have a script that makes any changes to the current schema (for upgrades)
For a small CMS, I'd just keep the SQL in a schema file and import it when I need it.
You could also do a database copy from your dev -> live system. So you make the changes in the dev database as you need them and then push them to the live database. Something like SQLCompare for SQL Server works well.
Wordpress does the install.php route, where you have to enter your credentials and such for the target database and it then pushes the changes to it.
If you're going to be distributing your application for 3rd parties to install on their own servers, a very common approach is to provide (as you said) a simple install.php file. If your application is more complicated, often times an installation directory will come packaged. The user installing the application opens this in a browser, where your script typically does a few things:
Check PHP installation - verify (using function_exists()) all the required functions (and thus libraries) are installed and available. Alert the user of anything missing.
Allow the user to enter their configuration parameters - application specific settings required. Typically database hostname, username & password.
Test database connection - if successful, load initial tables. Commonly you keep your base schema file stored as a SQL file, so the application pushes this through the native mysql client, or issues the individual SQL commands directly.

Categories