An Object Relational Model implementation.

0.3 2020-08-05 11:57 UTC

This package is auto-updated.

Last update: 2024-11-12 20:01:49 UTC


README

A framework agnostic Object Relational Model implementation, based on Laminas-Db, for Borsch Framework.

Installation

Via composer :

composer require borschphp/orm

Integration in Borsch Framework

Create a new file config/database.php file then add this content inside :

use Borsch\Container\Container;
use Borsch\Db\Db;
use Laminas\Db\Adapter\AdapterInterface;

/**
 * Setup the database informations into the Borsch\Db\Db class.
 * It will be used later to deal with models.
 *
 * @param Container $container
 */
return function (Container $container): void {
    Db::addConnection($container->get(AdapterInterface::class), 'default');
};

Open the file config/container.php then add your AdapterInterface (=database connection) definition.
Example (we've set some values in .env before) :

/*
 * Database definitions
 * --------------------
 *
 * Borsch uses the laminas-db package, please check it out for more information :
 *     https://docs.laminas.dev/laminas-db/adapter/
 * You can update the database information in the .env file.
 */
$container->set(AdapterInterface::class, function () {
    return new Adapter([
        'driver'   => env('DB_DRIVER'),
        'database' => env('DB_NAME'),
        'username' => env('DB_USER'),
        'password' => env('DB_PWD'),
        'hostname' => env('DB_HOST'),
        'port' => env('DB_PORT'),
        'charset' => env('DB_CHARSET'),
        'driver_options' => [
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
            PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
            PDO::ATTR_EMULATE_PREPARES => false,
            PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8mb4 COLLATE utf8mb4_general_ci'
        ]
    ]);
})->cache(true);

Open the file public/index.php, then load your database settings, below the container instance :

/** @var ContainerInterface $container */
$container = (require_once __DIR__.'/../config/container.php');

(require_once __DIR__.'/../config/database.php')($container); // <-- Here

Done 🎉 !

Usage

Create a class representing a table in your database which extends Borsch\ORM\Model.

use Borsch\ORM\Model;

/**
 * Class Album
 *
 * @property int $id
 * @property string $artist
 * @property string $title
 * @property string $artist_title
 * @property string $created_at
 * @property string $updated_at
 */
class Album extends Model
{

    protected $artist;
    protected $title;

    public function getCreatedAtProperty($date): DateTimeInterface
    {
        return new DateTime($date, new DateTimeZone('Europe/Paris'));
    }

    public function getArtistTitleProperty()
    {
        return sprintf('%s: %s', $this->artist, $this->title);
    }

    public function setUpdatedAtProperty($value)
    {
        if (is_numeric($value)) {
            $value = date('Y-m-d H:i:s', $value);
        } elseif ($value instanceof DateTimeInterface) {
            $value = $value->format('Y-m-d H:i:s');
        }

        $this->updated_at = $value;
    }
}

You can create accessors and mutators for your class properties (getCreatedAtProperty, setUpdatedAtProperty).
It is also possible to create accessors for no properties (getArtistTitleProperty).

Your class $id, $created_at and $updated are already set in Borsch\ORM\Model, you do not need to define them.

Active Records

You can easily create or update element in your database in an Active Record way :

$album = new Album();
$album->artist = 'Muse';
$album->title = 'The Resistance';
$album->save(); // Saved

$album = Album::where(['title' => 'The Resistance', 'artist' => 'Muse'])->first();
$album->title = 'Absolution';
$album->save(); // Updated

$album->delete(); // Deleted

Query Builder

As said, the package is based on Laminas-Db, therefore you can use the fluent SQL Abstraction to fetch your data.

License

The package is licensed under the MIT license. See License File for more information.