DAO

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

If the database is to be used in all http requests, the connection can be located in the app/config/services.php file:

try{
    \Ubiquity\orm\DAO::startDatabase($config);
}catch(Exception $e){
    echo $e->getMessage();
}

If the database is only used on a part of the application, it is better to create a base controller for that part, and implement the connection in its override initialize method:

app/controllers/ControllerWithDb.php
1
2
3
4
5
6
7
 namespace controllers;
 class ControllerWithDb extends ControllerBase{
     public function initialize(){
             $config=\Ubiquity\controllers\Startup::getConfig();
             \Ubiquity\orm\DAO::startDatabase($config);
     }
 }

Loading data

Loading an instance

Loading an instance of the models\User class with id 5

use Ubiquity\orm\DAO;

$user=DAO::getOne("models\User",5);

BelongsTo loading

By default, members defined by a belongsTo relationship are automatically loaded

Each user belongs to only one category:

$user=DAO::getOne("models\User",5);
echo $user->getCategory()->getName();

It is possible to prevent this default loading ; the third parameter allows the loading or not of belongsTo members:

$user=DAO::getOne("models\User",5, false);
echo $user->getCategory();// NULL

HasMany loading

Loading hasMany members must always be explicit ; the third parameter allows the explicit loading of members.

Each user has many groups:

$user=DAO::getOne("models\User",5,["groupes"]);
foreach($user->getGroupes() as $groupe){
    echo $groupe->getName()."<br>";
}

Composite primary key

Either the ProductDetail model corresponding to a product ordered on a command and whose primary key is composite:

app/models/Products.php
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
 namespace models;
 class ProductDetail{
     /**
      * @id
     */
     private $idProduct;

     /**
      * @id
     */
     private $idCommand;

     ...
 }

The second parameter $keyValues can be an array if the primary key is composite:

$productDetail=DAO::getOne("models\ProductDetail",[18,'BF327']);
echo 'Command:'.$productDetail->getCommande().'<br>';
echo 'Product:'.$productDetail->getProduct().'<br>';

Loading multiple objects

Loading instances of the User class:

$users=DAO::getAll("models\User");
foreach($users as $user){
    echo $user->getName()."<br>";
}

Querying using conditions

Simple queries

The condition parameter is equivalent to the WHERE part of an SQL statement:

$users=DAO::getAll(User::class,'firstName like "bren%" and not suspended',false);

To avoid SQL injections and benefit from the preparation of statements, it is preferable to perform a parameterized query:

$users=DAO::getAll(User::class,'firstName like ? and suspended= ?',false,['bren%',false]);

UQueries

The use of U-queries allows to set conditions on associate members:

Selection of users whose organization has the domain lecnam.net:

$users=DAO::uGetAll(User::class,'organization.domain= ?',false,['lecnam.net']);

It is possible to view the generated request in the logs (if logging is enabled):

../_images/uquery-users-log.png

The result can be verified by selecting all users in this organization:

$organization=DAO::getOne(Organization::class,'domain= ?',['users'],['lecnam.net']);
$users=$organization->getUsers();

The corresponding logs:

../_images/uquery-users-orga-log.png

Modifying data

Adding an instance

Adding an organization:

$orga=new Organization();
$orga->setName('Foo');
$orga->setDomain('foo.net');
if(DAO::save($orga)){
    echo $orga.' added in database';
}

Adding an instance of User, in an organization:

$orga=DAO::getOne(Organization::class, 1);
$user=new User();
$user->setFirstname('DOE');
$user->setLastname('John');
$user->setEmail('doe@bar.net');
$user->setOrganization($orga);
if(DAO::save($user)){
    echo $user.' added in database in '.$orga;
}

Updating an instance

First, the instance must be loaded:

$orga=DAO::getOne(Organization::class,'domain= ?',false,['foo.net']);
$orga->setAliases('foo.org');
if(DAO::save($orga)){
    echo $orga.' updated in database';
}

Deleting an instance

If the instance is loaded from database:

$orga=DAO::getOne(Organization::class,5,false);
if(DAO::remove($orga)){
    echo $orga.' deleted from database';
}

If the instance is not loaded, it is more appropriate to use the delete method:

if(DAO::delete(Organization::class,5)){
    echo 'Organization deleted from database';
}