b2pweb/bdf-prime

Bdf Prime ORM

v2.1.3 2024-12-02 15:20 UTC

README

Prime is a Data mapper ORM based on doctrine DBAL. The goal of prime is to lightweight usage of data mapper and doctrine DBAL.

build Scrutinizer Code Quality codecov Packagist Version Total Downloads Type Coverage

Getting Started

See Wiki for more information

composer require b2pweb/bdf-prime
<?php

use Bdf\Prime\ConnectionManager;
use Bdf\Prime\Entity\Model;
use Bdf\Prime\Mapper\Mapper;
use Bdf\Prime\Mapper\Builder\FieldBuilder;
use Bdf\Prime\Mapper\Builder\IndexBuilder;
use Bdf\Prime\Query\Expression\Like;
use Bdf\Prime\ServiceLocator;

// Declare your entity
class User extends Model
{
    public $id;
    public $firstName;
    public $lastName;
    public $email;

    public function __construct(array $data) 
    {
        $this->import($data);
    }
}

// Declare the data mapper for the entity
class UserMapper extends Mapper
{
    public function schema(): array
    {
        return [
            'connection' => 'myDB',
            'table'      => 'users',
        ];
    }
    
    public function buildFields(FieldBuilder $builder): void
    {
        $builder
            ->bigint('id')->autoincrement()
            ->string('firstName')
            ->string('lastName')
            ->string('email')
        ;
    }

    public function buildIndexes(IndexBuilder $builder): void
    {
        $builder->add()->on('name');
    }
}

// Declare your connections
$connexions = new ConnectionManager();
$connexions->declareConnection('myDB', 'mysql://myuser:mypassword@localhost');

// Use the service locator to locate your repositories
$manager = new ServiceLocator($connexions);
Locatorizable::configure($manager);
$repository = $manager->repository(User::class);

// Get and update an entity
$user = User::findById(1);
$user->setFirstName('john')->save();

// Use a query builder for searching entities 
User::where('firstName', 'john')->orWhere('email', (new Like('john%'))->startsWith())->all();