Skip to content
/ yadm Public
forked from formapro/yadm

An efficient way to treat MongoDB in PHP. Extremely fast persistence and hydration.

License

Notifications You must be signed in to change notification settings

eshcrow/yadm

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

92 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Yadm is the fastest MongoDB ODM.

Build Status

The schema less ODM. It gives you the fastest hydration and persistent. Based on makasim/values lib.

Install

$ composer require makasim/yadm "mikemccabe/json-patch-php:dev-master as 0.1.1"

Storage example

Let's say we have an order model:

<?php

namespace Acme;

use function Makasim\Values\set_value;
use function Makasim\Values\get_value;

class Price
{
    private $values = [];
    
    public function setCurrency(string $value): void
    {
        set_value($this, 'currency', $value);
    }
    
    public function getCurrency(): string 
    {
        return get_value($this, 'currency');
    }
    
    public function setAmount(int $value): void
    {
        set_value($this, 'amount', $value);
    }
    
    public function getAmount(): string 
    {
        return get_value($this, 'amount');
    }
}
<?php
namespace Acme;

use function Makasim\Values\set_value;
use function Makasim\Values\get_value;
use function Makasim\Values\set_object;
use function Makasim\Values\get_object;

class Order
{
    private $values = [];
    
    public function setNumber(string $number): void
    {
        set_value($this, 'number', $number);
    }
    
    public function getNumber(): string 
    {
        return get_value($this, 'number');
    }
    
    public function setPrice(Price $price): void
    {
        set_object($this, 'price', $price);
    }
    
    public function getPrice(): Price
    {
        return get_object($this, 'price', Price::class);
    }
}
<?php
namespace Acme;

use MongoDB\Client;
use Makasim\Yadm\Hydrator;
use Makasim\Yadm\Storage;

$collection = (new Client())->selectCollection('acme_demo', 'orders');
$hydrator = new Hydrator(Order::class);
$storage = new Storage($collection, $hydrator);

$price = new Price();
$price->setAmount(123); # 1.23 USD
$price->setCurrency('USD');

$order = new Order();
$order->setNumber(1234);
$order->setPrice($price);

$storage->insert($order);

$foundOrder = $storage->find(['_id' => get_object_id($order)]);
$foundOrder->setNumber(4321);
$storage->update($foundOrder);

$storage->delete($foundOrder);

MongoDB special types usage

<?php
namespace Acme;

use MongoDB\Client;
use Makasim\Yadm\Hydrator;
use Makasim\Yadm\Storage;
use Makasim\Yadm\ConvertValues;
use Makasim\Yadm\Type\UuidType;
use Makasim\Yadm\Type\UTCDatetimeType;
use Makasim\Yadm\Uuid;
use function Makasim\Values\set_value;
use function Makasim\Values\get_value;

$convertValues = new ConvertValues([
    'id' => new UuidType(),
    'createdAt' => new UTCDatetimeType(),
]);

$collection = (new Client())->selectCollection('acme_demo', 'orders');
$hydrator = new Hydrator(Order::class);
$storage = new Storage($collection, $hydrator, null, null, $convertValues);
 

$order = new Order();
set_value($order, 'id', Uuid::generate()->toString());
set_value($order, 'createdAt', (new \DateTime())->format('U'));

$storage->insert($order);

$id = get_value($order, 'id');

// find by uuid
$anotherOrder = $storage->findOne(['id' => new Uuid($id)]);

// do not update id if not changed
$storage->update($anotherOrder);

// update on change
set_value($anotherOrder, 'id', Uuid::generate()->toString());
$storage->update($anotherOrder);

Other examples

In makasim/values repo you can find examples on how to build simple objects, object trees, hydrate and retrive data from\to object.

Benchmarks

License

MIT

About

An efficient way to treat MongoDB in PHP. Extremely fast persistence and hydration.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%