-
Notifications
You must be signed in to change notification settings - Fork 60
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[RFC] Allow multiple databases #60
Comments
For the moment, the connection to the database is done by: It is also possible to do this in a controller by: What do you think about the following approach? With only one database
With several databases configured as you propose
NoteI prefer to leave this explicit startup, rather than associate a Model class with a database connection. This choice excludes the possibility of querying data from 2 databases on the same server |
I think this works fine, but, I will need to start specific database every time I need to do some operation with those databases. I think is a model responsability to know wich database it has to connect before query. Also a model doesn't need to connect to database, it could use a Webservice or other way to retrieve/save data, so you could have a model that use a Rest API to perform CRUD operations, and in this case your "property useDatabase" could be set to false. Plus inside your controller you also would be able to change connection before query using $model->setDatabaseConfig([....]); |
if the model is responsible for knowing which database to use, it may be considered to do so in this way: namespace models;
/**
* @database("extra1")
*/
class User{
} namespace models;
// Use default database
class Products{
} In this case, the connection must be made automatically as soon as a model is requested, without the developer having to make an explicit call to //Starts extra1 connection for the first time
$users=DAO::getAll(User::class);
//Re-use extra1 connection
DAO::update($user);
//Starts default connection
$product=DAO::getById(Product::class,1); I think it's more practical, but it can have a slight impact on performance (to be benchmarked). |
I like the proposed implementation. I think it continues transparent with the @database declaration and developers still can change database in controller when needed. Now, is to run bench tests to see it is viable. |
Good News With TechEmpower Multiple database queries benchmark (with 20 queries): There is a little more discrepancy with my test procedure (but it solicits more the ORM on the relationship and multi-table aspects). Now we have to integrate this in the Models part of the Webtools, which should allow us to navigate between the databases. This feature will be integrated in Ubiquity 2.3.0 |
Good news and a great job as always. |
Hi @gildonei ,
You can then create a new project integrating multi-db:
The addition of a connection can be done by webtools, provided that the default connection is already operational: |
The button to add new connection requested the URL, but form wasn't displayed at the page Also, creating a model to a table on default database, generate an error on cache because all table names are lowercase and cache generation is considering first letter uppercase
|
|
Can you tell me exactly what you do after the project is created:
|
It's okay, I reproduced your bug: You can't create a new connection if for the default connection, the models are not generated, and the cache is not initialized (the new connection button should have been disabled) |
a) cd test-multi |
On your screenshot, the models cache is not generated for default connection The default connection must be ok before creating a new connection. |
I just have default connection, couldn't add any other. Also tried do delete (from windows explorer) all cache files, but models page keep blank. |
Solved model blank error page. It was my mistake. I add an annotation to specify table name and it had an double arrow "==>) causing error |
Done! I don't think is necessary to create Models inside a subfolder, as the database name is explicity declared at the model. |
I don't understand this remark. You mean there's no point in creating the class But you can have the same class (table) in 2 different databases! |
Understood why you decided to use subfolder. I agree with that. This way you can use the namespaces to have 2 classes with same name, but in different namespaces. I think that now, we can close this issue and wait to integrate it to the next release. |
It also remains to implement acceptance and unit tests, |
OK, I will try to help with docs |
Summary
Allow framework to use multiple databases.
Motivation
Some projects may need to utilize different database connections on same project, so, Ubiquity should allow to configure a "default" database and extra connections.
Compatibility with Ubiquity's philosophy
Indicate compatibility or improvements in:
Expected results
Be able to connect to different databases
Additional context
Models must be able to specify wich database will be use. If none was specified, it uses default database connection
config.php
The text was updated successfully, but these errors were encountered: