2017 © Pedro Peláez
 

library stash

Stash: Mongo ODM

image

potfur/stash

Stash: Mongo ODM

  • Monday, September 7, 2015
  • by potfur
  • Repository
  • 0 Watchers
  • 6 Stars
  • 0 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

Stash - MongoDB ODM

Stash is an object-document mapper for MongoDB written in PHP. It adds fully transparent persistence layer while still preserving easy way how MongoDB is handled., (*1)

This means that MongoDB can be used in almost exact same way as it would be used with arrays. But with small but important differences - instead of returning plain arrays, Stash will return objects - entites. Since Stash returns entities it also can store them., (*2)

Next steps

  • event dispatcher
  • references

Example

Model definitions, (*3)

$models = new ModelCollection();
$models->register(
    new Model(
        '\Order',
        [
            new Id(),
            new Document('customer'),
            new ArrayOf('items', Fields::TYPE_DOCUMENT)
        ]
    ),
    'order'
);

$models->register(
    new Model(
        '\OrderItem',
        [
            new Scalar('name', Fields::TYPE_STRING),
            new Scalar('amount', Fields::TYPE_INTEGER),
            new Scalar('cost', Fields::TYPE_INTEGER)
        ]
    )
);

$models->register(
    new Model(
        '\Voucher',
        [
            new Scalar('name', Fields::TYPE_STRING),
            new Scalar('cost', Fields::TYPE_INTEGER)
        ]
    )
);

$models->register(
    new Model(
        '\Customer',
        [
            new Scalar('name', Fields::TYPE_STRING),
            new Document('address')
        ]
    )
);

$models->register(
    new Model(
        '\CustomerAddress',
        [
            new Scalar('address', Fields::TYPE_STRING),
            new Scalar('city', Fields::TYPE_STRING),
            new Scalar('zip', Fields::TYPE_STRING)
        ]
    )
);

Connection to database:, (*4)

$connection = new Connection(
    new MongoClient(),
    'test',
    new DocumentConverter(new Converter(), $models),
    $models
);

Creating entity and how to store it:, (*5)

class Order
{
    private $id;
    private $customer;
    private $items;

    public function __construct($customer, $items)
    {
        $this->customer = $customer;
        $this->items = $items;
    }
}

class OrderItem
{
    private $name;
    private $amount;
    private $cost;

    public function __construct($name, $amount, $cost)
    {
        $this->name = $name;
        $this->amount = $amount;
        $this->cost = $cost;
    }
}

class Voucher
{
    private $name;
    private $discount;

    public function __construct($name, $discount)
    {
        $this->name = $name;
        $this->discount = $discount;
    }
}

class Customer
{
    private $name;
    private $address;

    public function __construct($name, CustomerAddress $address)
    {
        $this->$name = $name;
        $this->address = $address;
    }
}

class CustomerAddress
{
    private $address;
    private $city;
    private $zip;

    public function __construct($address, $city, $zip)
    {
        $this->address = $address;
        $this->city = $city;
        $this->zip = $zip;
    }
}

$order = new Order(
    new Customer('Joe Doe', new CustomerAddress('Mongo alley', 'Somewhere', '12345')),
    [
        new OrderItem('Foos', 10, 1000),
        new Voucher('Voucher', 250)
    ]
);

$connection->getCollection('order')->save($order);

And this is stored BSON representation. When saving objects - entities Stash adds _class field, where it stores class name, (*6)

{
  "_id" : ObjectId("55746f4f87dee7bc0b000033"),
  "_class" : "Order",
  "customer" : {
    "_class" : "Customer",
    "address" : {
      "_class" : "CustomerAddress",
      "address" : "Mongo alley",
      "city" : "Somewhere",
      "zip" : "12345"
    }
  },
  "items" : [
    {
      "_class" : "OrderItem",
      "name" : "Foos",
      "amount" : 10,
      "cost" : 1000
    },
    {
      "_class" : "Voucher",
      "name" : "Voucher",
      "discount" : 250
    }
  ]
}                                                     

The Versions

07/09 2015

dev-dev

dev-dev

Stash: Mongo ODM

  Sources   Download

MIT

The Requires

 

The Development Requires

by Michal Wachowski

database mongo odm persistence document

23/08 2015

1.0.0-alpha

1.0.0.0-alpha

Stash: Mongo ODM

  Sources   Download

MIT

The Requires

 

The Development Requires

by Michal Wachowski

database mongo odm persistence document