Menu

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.

Use statements and class header omitted
class Plant implements AnnotatedInterface
{

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

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

	use I18NAbleTrait;

	/**
	 * @Sanitizer(MongoObjectId)
	 * @see MongoObjectId
	 * @var MongoId
	 */
	public $_id;

	/**
	 * @I18N
	 * @var string
	 */
	public $name = '';

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

	use I18NAbleTrait,
	  FinderTrait;

	/**
	 * @Sanitizer(MongoObjectId)
	 * @see MongoObjectId
	 * @var MongoId
	 */
	public $_id;

	/**
	 * @I18N
	 * @var string
	 */
	public $name = '';

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

	use I18NAbleTrait,
	  FinderTrait,
	  EntityManagerTrait;

	/**
	 * @Sanitizer(MongoObjectId)
	 * @see MongoObjectId
	 * @var MongoId
	 */
	public $_id;

	/**
	 * @I18N
	 * @var string
	 */
	public $name = '';

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

	use I18NAbleTrait,
	  FinderTrait,
	  EntityManagerTrait,
	  OwneredTrait;

	/**
	 * @Sanitizer(MongoObjectId)
	 * @see MongoObjectId
	 * @var MongoId
	 */
	public $_id;

	/**
	 * @I18N
	 * @var string
	 */
	public $name = '';

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

	use I18NAbleTrait,
	  FinderTrait,
	  EntityManagerTrait,
	  OwneredTrait,
	  SimpleTreeTrait;

	/**
	 * @Sanitizer(MongoObjectId)
	 * @see MongoObjectId
	 * @var MongoId
	 */
	public $_id;

	/**
	 * @I18N
	 * @var string
	 */
	public $name = '';

	public function __construct()
	{
		$this->initTree();
	}

}

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.

MongoId 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 MongoId;
$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' => '5a12ba4409b006cc7df15b65',
  'firstName' => 'Fanny',
  'lastName' => 'Corkery',
  'email' => 'umosciski@kutch.info',
  'address' => 
  array (
    'city' => 'East Felipeborough',
    'street' => 'Franecki Rest',
    'home' => 9,
    'appartment' => 6,
    '_class' => 'Maslosoft\\ManganExamples\\Embedded\\Address',
  ),
  '_class' => 'Maslosoft\\ManganExamples\\Embedded\\Customer',
)

Model converted to JSON string

{
    "_id": "5a12ba4409b006cc7df15b65",
    "firstName": "Fanny",
    "lastName": "Corkery",
    "email": "umosciski@kutch.info",
    "address": {
        "city": "East Felipeborough",
        "street": "Franecki Rest",
        "home": 9,
        "appartment": 6,
        "_class": "Maslosoft\\ManganExamples\\Embedded\\Address"
    },
    "_class": "Maslosoft\\ManganExamples\\Embedded\\Customer"
}

Model converted to YAML string

_id: 5a12ba4409b006cc7df15b65
firstName: Fanny
lastName: Corkery
email: umosciski@kutch.info
address:
    city: 'East Felipeborough'
    street: 'Franecki Rest'
    home: 9
    appartment: 6
    _class: Maslosoft\ManganExamples\Embedded\Address
_class: Maslosoft\ManganExamples\Embedded\Customer

Model rendered as Datamatrix Barcode image

Model converted to datamatrix barcode

  • Projects