Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Basic Usage

MiniCodeMonkey edited this page · 10 revisions
Clone this wiki locally

Basic Usage

This tutorial guides you through the basic operations of Morph

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

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 Objects

Saving and retrieving of object is achieved through the object itself.

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..
\morph\Storage::init($mongo->selectDB('MyDatabase'));

$aBook = new Book();
$aBook->title = 'MongoDB Rocks!';
$aBook->author = 'Jonathan Moss';
$aBook->pageNumber = 150;
$aBook->publishedDate = strtotime('2009-09-12');

$aBook->save();

To retrieve the a book object by it's id you just need to do the following:

$mongo = new Mongo();
\morph\Storage::init($mongo->selectDB('MyDatabase'));

$aBook = new Book();
$aBook->loadById(new \MongoId('some_big_long_id'));

echo $aBook; //all \morph\Object derived classes inherit a __toString() method which is useful for debugging.

It is worth noting the use of new \MongoId() when loading by id. By default Morph (and MongoDB itself) use a special object for Ids \MongoId(). So unless you specify your own id to use, then MongoDB will use ObjectID's which will require you to wrap your id's in a MongoID() when searching for them.

This means that when you fetch objects using Ids you need to make sure you are using instance of MongoId(), not just strings.

Querying the Database

\morph\Object derived classes 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();
\morph\Storage::init($mongo->selectDB('MyDatabase'));

$aBook = new Book();

$query = new \morph\Query();
$query->property('author')->equals('Jonathan Moss');

$resultsSet = $aBook->findByQuery($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();
\morph\Storage::init($mongo->selectDB('MyDatabase'));

$aBook = new Book();

$query = new \morph\Query();
$query->property('author')
      ->equals('Jonathan Moss')
      ->property('pageNumber')
      ->lessThan(1000)
      ->greaterThan(50);

$resultsSet = $aBook->findByQuery($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";
}
Something went wrong with that request. Please try again.