See example repository for working example
As MongoDB comes with storage engine capable of storing nested structures, Mangan has facility to store objects compositions. Most simple solution for this is to embed sub object in the same document, just like it is created at runtime.
To store sub object in database, use
annotation on one of object property:
/** * @Embedded(Address) */ public $address = null;
By default Mangan will ensure instance when transforming model. To
null as value, the
nullable option can be set to true:
/** * @Embedded(Address, 'nullable' = true) */ public $address = null;
@Nullable annotation can be used:
/** * @Embedded(Address) * @Nullable */ public $address = null;
The property marked as
@Embedded can store and
retrieve object, for example:
$address = new Address; $address->city = 'Sao Paulo'; $address->street = 'New street'; $company = new Company; $company->name = 'Maslosoft'; $company->address = $address; $saved = $company->save();
To retrieve object to same state as it were before saving, use find method.
Document class, this has convenient active document
method for finding it.
Example of finding any document in collection using active document approach:
$company = Company::model()->find(); echo $company->name; // Maslosoft echo $company->address->city; // Sao Paulo
(new EntityManager($company))->save(); $found = (new Finder(new Company))->find() // Company instance;