Documentation/Container/Services/DB

DB Service — Using Databases

The DB service provides the initial capability to send queries to databases. Using a wrapper over PDO and the database configuration of the HLEB2 framework, the service offers simple methods to interact with various databases (supported by PDO).

The PHP PDO extension and necessary database drivers must be enabled for this service to work.

To use a different connection method, such as ORM(Object-Relational Mapping), add the instantiation of the chosen ORM as a service container using the framework's configuration settings.

According to the project's structure provided with the HLEB2 framework, the DB service can only be used in Model classes.
A Model class (whose template can be created using a console command) acts as a basic framework for use within MVC (Action-Domain-Responder for web). It can be adapted or replaced according to preference for the selected AR(Active Record) or ORM library (and then adjust the template for the console command).

Examples of usage in a Model for database queries:

<?php
// File /app/Models/ExampleModel.php

namespace App\Models;

use 
Hleb\Base\Model;
use 
Hleb\Reference\DbInterface;
use 
Hleb\Static\DB;

class 
ExampleModel extends Model
{
    public static function 
get(): false|array
    {
        
$query '"SELECT * FROM table_name WHERE active=1';

        
// variant 1
        
$data self::container()->get(DbInterface::class)->dbQuery($query);

        
// variant 2
        
$data self::container()->db()->dbQuery($query);

        
// variant 3
        
$data DB::dbQuery($query);

        return 
$data;
    }
}

The following methods of the DB service are used for executing database queries.


#dbQuery()

The dbQuery() method was used in the examples above for creating direct SQL queries to the database. The query and query parameters are not separated in it, so every suspicious parameter, especially those coming from a Request, must be handled (with proper escaping) using the special quote() method.

use Hleb\Static\DB;

$result DB::dbQuery(sprintf("SELECT * FROM users WHERE name='%s' AND address='%s'"DB::quote($name), DB::quote($address)));

Escaping query parameters ensures protection against SQL injection. Such attacks are based on injecting arbitrary SQL expressions as part of external data.

Another method of the DB service is more versatile and simplifies parameter handling.


#run()

When successfully executed, the run() method returns an initialized PDOStatement object. All methods of this object, such as fetch() and fetchColumn(), are standard for PDO.

use Hleb\Static\DB;

$result DB::run("SELECT * FROM users WHERE name=? AND address=?", [$name$address])->fetchAll();

The capabilities of PDOStatement are described in the PDO documentation.


#Asynchronous Queries

For asynchronous queries, using this service is similar and depends on the configuration of the web server in use.

Additionally, some ORMs are adapted to support this mode of operation.

One such library, as indicated in its documentation, is Cycle ORM.

Path Sessions

Page translated: chatgpt 4-o
Back to top