PHP code example of b2pweb / bdf-prime

1. Go to this page and download the library: Download b2pweb/bdf-prime library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

b2pweb / bdf-prime example snippets




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();