b2pweb/bdf-prime

Bdf Prime ORM

v2.0.5 2023-02-02 15:25 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 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 $name;
    public $password;
    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('name')
            ->string('password')
            ->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->setName('john')->save();

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