The schema less ODM. It gives you the fastest hydration and persistent. Based on formapro/values lib.
$ composer require formapro/yadm "mikemccabe/json-patch-php:dev-master as 0.1.1"
Let's say we have an order model:
<?php
namespace Acme;
use function Formapro\Values\set_value;
use function Formapro\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 Formapro\Values\set_value;
use function Formapro\Values\get_value;
use function Formapro\Values\set_object;
use function Formapro\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 Formapro\Yadm\Hydrator;
use Formapro\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);
<?php
namespace Acme;
use MongoDB\Client;
use Formapro\Yadm\Hydrator;
use Formapro\Yadm\Storage;
use Formapro\Yadm\ConvertValues;
use Formapro\Yadm\Type\UuidType;
use Formapro\Yadm\Type\UTCDatetimeType;
use Formapro\Yadm\Uuid;
use function Formapro\Values\set_value;
use function Formapro\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);
In formapro/values repo you can find examples on how to build simple objects, object trees, hydrate and retrive data from\to object.
MIT