Basic UsageThis tutorial guides you through the basic operations of Morph Defining Storable objectDefining objects that can be stored in MongoDB is quite straight forward. All you need to do is write classes that extend Morph_Object and specifying the fields that it should store using the addProperty function(). For example:
class Book extends Morph_Object
{
public function __construct($id = null)
{
parent::__construct($id);
$this->addProperty(new Morph_Property_String('title'))
->addProperty(new Morph_Property_String('author'))
->addProperty(new Morph_Property_Integer('pageNumber'))
->addProperty(new Morph_Property_Date('publishedDate'));
}
}
The class defined above represents a book and holds 4 storable properties: - title - A string to hold the title of the book
- author - A string to hold the author of the book
- pageNumber - An integer to hold the number of pages in the book
- published - A date to hold the date that the book was published
Saving and Retrieving ObjectsSaving and retrieving of object is achieved through the Morph_Storage object. The following example code show how to create a new Book object and save it to the database 'MyDatabase'
$mongo = new Mongo(); //or MongoAuth etc..
$storage = new Morph_Storage($mongo->selectDB('MyDatabase'));
$aBook = new Book();
$aBook->title = 'MongoDB Rocks!';
$aBook->author = 'Jonathan Moss';
$aBook->pageNumber = 150;
$aBook->publishedDate = strtotime('2009-09-12');
$storage->save($aBook);
To retrieve the a book object by it's id you just need to do the following:
$mongo = new Mongo();
$storage = new Morph_Storage($mongo->selectDB('MyDatabase'));
$aBook = new Book();
$storage->fetchById($aBook, 'MyIdNumber1');
echo $aBook; //all Morph_Object derived classes inherit a __toString() method which is useful for debugging.
Querying the DatabaseMorph_Storage also provides the method findByQuery which will find objects in MongoDB and return PHP objects. The key to this method is the class Morph_Query which can be used to define a query in an object oriented fashion. e.g.:
$mongo = new Mongo();
$storage = new Morph_Storage($mongo->selectDB('MyDatabase'));
$aBook = new Book();
$query = new Morph_Query();
$query->property('author')->equals('Jonathan Moss');
$resultsSet = $storage->findByQuery($aBook, $query);
You can also build more complex queries consisting of several constraints such as the one below which will return all Book objects that have the author 'Jonathan Moss' and have a pageNumber between 50 and 1000.
$mongo = new Mongo();
$storage = new Morph_Storage($mongo->selectDB('MyDatabase'));
$aBook = new Book();
$query = new Morph_Query();
$query->property('author')
->equals('Jonathan Moss')
->property('pageNumber')
->lessThan(1000)
->greaterThan(50);
$resultsSet = $storage->findByQuery($aBook, $query);
findByQuery() does not return all the results straight away. Instead it provides an instance of Morph_Iterator. Morph_Iterator implements the Iterator interface and can therefore be used most easily in foreach loops. For example:
foreach($resultSet as $aBook) {
echo $aBook->title . "\n";
}
|
$mongo = new Mongo(); //or MongoAuth? etc.. $storage = new Morph_Storage?($storage->selectDB('MyDatabase?'));
should read
$mongo = new Mongo(); //or MongoAuth? etc.. $storage = new Morph_Storage?($mongo->selectDB('MyDatabase?'));
Thank Alexander,
Corrected the typo