Skip to content

MongoRecord: a very light-weight Active Record-esque ODM for MongoDB in PHP 5.3

License

Notifications You must be signed in to change notification settings

DarrenN/MongoRecord

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MongoRecord – PHP 5.3 Namespaced & Autoloaded Version

MongoRecord is a PHP Mongo ORM layer built on top of the PHP Mongo PECL extension

MongoRecord is an extraction from online classifieds site Oodle. Oodle’s requirements for a manageable, easy to understand interface for dealing with the super-scalable Mongo datastore was the primary reason for MongoRecord. It was developed to use with PHP applications looking to add Mongo’s scaling capabilities while dealing with a nice abstraction layer.

Additions to Original:

  1. Using Autoloader instead of require
  2. Using PHP 5.3 Namespaces based on the PHP Standard Working Group Cabal – http://groups.google.com/group/php-standards/web/psr-0-final-proposal
  3. Models broken out into own directory, but this is optional

To-dos:

  • Write proper unit tests
  • Document code better
  • Write more examples

Features

  • Collection names by convention
  • Attributes by convention
  • Validations
  • Callbacks
  • Sorting, offsets, limits

Requirements

  • PHP 5.3+
  • Mongo PECL

Installation

Extract the source files into a directory in your PHP library path. Use SplClassAutloader.php to load classes using their namespaces:

$libDir = __DIR__ . '/lib';
$modelDir = __DIR__;
//require $libDir . '/MongoRecord/BaseMongoRecord.php';
require $libDir . '/SplClassLoader.php';
use MongoRecord\BaseMongoRecord,
    Models;
$classLoader = new SplClassLoader('MongoRecord', $libDir);
$classLoader->register();
$classLoader = new SplClassLoader('Models', $modelDir);
$classLoader->register();

Usage

Basic

Using MongoRecord is as simple as declaring classes that are extensions of the base ORM class. Models are currently in the Models directory but this can be changed accordingly in the autoloader.

namespace Models;
class Person extends \MongoRecord\BaseMongoRecord
{
}
// initialize connection and database name
BaseMongoRecord::$connection = new Mongo();
BaseMongoRecord::$database = 'myapp';

This gives Person basic CRUD methods: save(), destroy(), findOne(), and find().

Every class automatically gets mapped to a Mongo collection by convention.

E.g.
Personpeople
MyClassmy_classes

Creating and Fetching

New records can be created by instantiating and saving:

$person = new Models\Person();
$person->save(); // true or false depending on success
$person = Models\Person::findOne();
$person->destroy();

You can also add options to how you want to find.

// find the first Person sorted by name, starting from the tenth
Models\Person::find(array(), array('sort' => array('name' => 1), 'offset' => 10, 'limit' => 1));

Attributes

Attributes can be set in bulk on the constructor, one-by-one, or chained.

$person = new Models\Person(array('name' => 'Bob', 'description' => 'foobar'));
$person->setAge(25)->setGender("Male");
$person->save(); // returns true or false
Models\Person::find(array('name' => 'Bob', 'gender' => 'Male')); // finds all male Bobs in the people collection.

Validations

Validations can be added based on the name of the attribute

class Person extends \MongoRecord\BaseMongoRecord
{
    public function validatesName($name)
    {
        if ($name == 'Bob')
            return false;
        else
            return true;
    }
}
$person = new Models\Person();
$person->setName("Bob");
$person->save(); // fails!

Callbacks

Callbacks can be added for the following events:

  • beforeSave()
  • afterSave()
  • beforeValidation()
  • afterValidation()
  • beforeDestroy()
  • afterNew()

In a new, save, destroy cycle, the validations are called in the following order:

afterNew -> beforeValidation -> afterValidation -> beforeSave -> afterSave -> beforeDestroy

class Person extends \MongoRecord\BaseMongoRecord
{
    public function beforeSave()
    {
         if ($this->getName() == 'Bob')
             $this->setName('Bill');
    }
}
p.

About

MongoRecord: a very light-weight Active Record-esque ODM for MongoDB in PHP 5.3

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%