My favorites | Sign in
Project Logo
                
Search
for
Updated Jun 29, 2009 by XirisR
Labels: Featured, Phase-Implementation
BasicUsage  
Basic usage of the Morph package

Basic Usage

This tutorial guides you through the basic operations of Morph

Defining Storable object

Defining 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:

Saving and Retrieving Objects

Saving 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 Database

Morph_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";
}

Comment by alexan...@mindey.se, Jun 24, 2009

$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?'));

Comment by XirisR, Jun 29, 2009

Thank Alexander,

Corrected the typo


Sign in to add a comment
Hosted by Google Code