Database¶
The DAO class is responsible for loading and persistence operations on models :
Connecting to the database¶
Check that the database connection parameters are correctly entered in the configuration file:
Ubiquity config -f=database

Transparent connection¶
Since Ubiquity 2.3.0, The connection to the database is done automatically the first time you request it:
use Ubiquity\orm\DAO;
$firstUser=DAO::getById(User::class,1);//Automatically start the database
This is the case for all methods in the DAO class used to perform CRUD operations.
Explicit connection¶
In some cases, however, it may be useful to make an explicit connection to the database, especially to check the connection.
use Ubiquity\orm\DAO;
use Ubiquity\controllers\Startup;
...
try{
$config=\Ubiquity\controllers\Startup::getConfig();
DAO::startDatabase($config);
$users=DAO::getAll(User::class,'');
}catch(Exception $e){
echo $e->getMessage();
}
Multiple connections¶
Adding a new connection¶
Ubiquity allows you to manage several connections to databases.
With Webtools¶
In the Models part, choose Add new connection button:

Define the connection configuration parameters:

Generate models for the new connection:
The generated models include the @database
annotation or the Database
attribute mentioning their link to the connection.
<?php
namespace models\tests;
use Ubiquity\attributes\items\Database;
use Ubiquity\attributes\items\Table;
#[Database('tests')]
#[Table('groupe')]
class Groupe{
...
}
<?php
namespace models\tests;
/**
* @database('tests')
* @table('groupe')
*/
class Groupe{
...
}
Models are generated in a sub-folder of models
.
With several connections, do not forget to add the following line to the services.php
file:
\Ubiquity\orm\DAO::start();
The start
method performs the match between each model and its associated connection.