Maslosoft Mangan Logo Maslosoft Mangan
MongoDB Object Persister 

Unobtrusive Object Document Mapper

One could call mangan ODM which is in fact true, but mapping related tasks are at absolute minimum here. Document model itself is a plain php object with mostly optional annotations. Additional traits can be used to enhance models with features and methods.

Embedding

Embedding other documents is as easy as setting property of root object with other object or array of objects. To inform mangan that it's embedded document or array of documents @Embedded annotation or @EmbeddedArray annotations respectivelly, can be used.

Database references

Database references allows you to store part of objects hierarchy in separate collection or even database.

Data sanitizing

To prevent storing and transmitting of malicious or bogus data, mangan will automatically set values to proper types based on either built in sanitizers or user defined ones.

Built-in tree structures

There are three aout of the box tree structures for different use cases and different storage.

Object trashing

While it is required to delete some data from time to time, there is available facilities to move objects to trash collection and in case if document is need to be restored, it's pretty easy to do so.

Plain PHP object or Active Record

Decide by yourself to use simple or existing object, use Mangan Active Document implementation or blend it as you like. Thanks to trait's design Mangan document model can be build from existing or custom built blocks. Simply `use` feature trait to add feature to model.

Store and convert objects

Mangan not only allows you to store PHP object into MongoDB, but allows seamless two-way conversion between array, json or even YAML or DataMatrix Code image. Mangan will take care of proper data types and structure.

ObjectId will be stored in MongoDB as ObjectId even if it comes from JSON as string. Other types will be automatically sanitized, based on initial type, or by assigning sanitizer with annotation. Custom conversions can be added as necessary.

Example model

It is converted right here, right now (reload to get new data):

Use statements and class header omitted
class Customer implements AnnotatedInterface
{

	/**
	 * @Sanitizer(MongoObjectId)
	 * @see MongoObjectId
	 * @var MongoId
	 */
	public $_id;
	public $firstName = '';
	public $lastName = '';
	public $email = '';

	/**
	 * @Embedded(Address)
	 * @var Address
	 */
	public $address = null;

}
Use statements and class header omitted
class Address implements AnnotatedInterface
{

	public $city = '';
	public $street = '';
	public $home = 0;
	public $appartment = 0;

}
$faker = Factory::create();
$model = new Customer;
$model->_id = new ObjectId;
$model->firstName = $faker->firstName;
$model->lastName = $faker->lastName;
$model->email = $faker->email;
$address = new Address;
$address->appartment = $faker->randomDigit;
$address->home = $faker->randomDigit;
$address->street = $faker->streetName;
$address->city = $faker->city;
$model->address = $address;

Array representaion of model

array (
  '_id' => '670059b5e4075c39d201361c',
  'firstName' => 'Erica',
  'lastName' => 'Hayes',
  'email' => 'marvin.mateo@yahoo.com',
  'address' => 
  array (
    'city' => 'South Kyler',
    'street' => 'Kerluke Rapid',
    'home' => 8,
    'appartment' => 5,
    '_class' => 'Maslosoft\\ManganExamples\\Embedded\\Address',
  ),
  '_class' => 'Maslosoft\\ManganExamples\\Embedded\\Customer',
)

Model converted to JSON string

{
    "_id": "670059b5e4075c39d201361c",
    "firstName": "Erica",
    "lastName": "Hayes",
    "email": "marvin.mateo@yahoo.com",
    "address": {
        "city": "South Kyler",
        "street": "Kerluke Rapid",
        "home": 8,
        "appartment": 5,
        "_class": "Maslosoft\\ManganExamples\\Embedded\\Address"
    },
    "_class": "Maslosoft\\ManganExamples\\Embedded\\Customer"
}

Model converted to YAML string

_id: 670059b5e4075c39d201361c
firstName: Erica
lastName: Hayes
email: marvin.mateo@yahoo.com
address:
    city: 'South Kyler'
    street: 'Kerluke Rapid'
    home: 8
    appartment: 5
    _class: Maslosoft\ManganExamples\Embedded\Address
_class: Maslosoft\ManganExamples\Embedded\Customer
* Tabs and slider tabs used here comes from Maslosoft Framework