1. Go to this page and download the library: Download retentio/boomgo library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
retentio / boomgo example snippets
namespace VendorName\Project\Document;
class MyPersistedClass
{
/**
* @Persistent
*/
private $myField
public function getMyField()
{
return $this->myField;
}
public function setMyField($value)
{
$this->myField = $value;
}
}
// Create your connection with the native mongoDB php driver
$mongo = new \MongoClient("mongodb://127.0.0.1:27017");
// Create your object
$object = new \VendorName\Project\Document\MyPersistedClass();
$object->setMyField('my value');
// Create the mapper
$mapper = new \VendorName\Project\Mapper\MyPersistedClassMapper();
// Serialize your object to a mongoable array
$mongoableArray = $mapper->serialize($object);
// Save with the native php driver
$mongo->selectDB('my_db')
->selectCollection('my_collection')
->save($mongoableArray);
// Fetch a result with the native driver
$result = $mongo->selectDB('my_db')
->selectCollection('my_collection')
->findOne(array('myField' => 'my value'));
// Unserialize the result to an object
$object = $mapper->unserialize($result);
$object->getMyField();
// You could also hydrate an existing object from a result
$object = new \VendorName\Project\Document\MyPersistedClass();
$mapper->hydrate($object, $result);
namespace VendorName\Project\Document;
class DocumentClass
{
/**
* @Persistent
* @var \MongoId
*/
private $id // expect a MongoId native instance
/**
* @Persistent
* @var string
*/
private $myField // scalar type should be specified, avoid normalization process
/**
* @Persistent
* @var array
*/
private $myArray // composite type will be normalized
/**
* @Persistent
*/
private $myVar // Default type will be "mixed" and processed as a composite type
/**
* @Persistent
* @var VendorName\Project\EmbeddedDocument
*/
private $embeddedDocument // a single embedded document
/**
* @Persistent
* @var array [VendorName\Project\EmbeddedDocument]
*/
private $embeddedCollection // many embedded documents the "[ ]" chars are mandatory
// getters & setters
}
// Create your connection with the native mongoDB php driver
$mongo = new \MongoClient("mongodb://127.0.0.1:27017");
// Create your object
$object = new \VendorName\Project\Document\DocumentClass();
$object->setId(new \MongoId());
$object->setMyField('my value');
$object->setMyArray(array('many','values'));
$object->setMyVar('anything');
$object->setEmbeddedDocument(new \VendorName\Project\Document\EmbeddedDocument());
$object->setEmbeddedCollection(array(new \VendorName\Project\Document\EmbeddedDocument()));
// Create the mapper
$mapper = new \VendorName\Project\Mapper\DocumentClassMapper();
// Serialize your object to a mongoable array
$mongoableArray = $mapper->serialize($object);
// Save with the native php driver
$mongo->selectDB('my_db')
->selectCollection('my_collection')
->save($mongoableArray);
// Fetch a result with the native driver
$result = $mongo->selectDB('my_db')
->selectCollection('my_collection')
->findOne(array('myField' => 'my value'));
// Unserialize the result to an object
$object = $mapper->unserialize($result);