GithubHelp home page GithubHelp logo

macedd / mongorecord Goto Github PK

View Code? Open in Web Editor NEW

This project forked from lunaru/mongorecord

0.0 2.0 0.0 169 KB

MongoRecord is a simple, easy to set-up Mongo ORM for PHP with ActiveRecord-like features.

License: MIT License

PHP 100.00%

mongorecord's Introduction

MongoRecord

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.

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.

Usage

Basic

Using MongoRecord is as simple as declaring classes that are extensions of the base ORM class.

class Person extends 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

It is possible to set collection name manualy for child class.
You can do it by overriding class name as in example.

class PersonClassNameIsTooComplicated extends BaseMongoRecord
{
    protected static $collectionName = 'person';
}

Creating and Fetching

New records can be created by instantiating and saving:

$person = new Person();
$person->save(); // true or false depending on success
$person = 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
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 Person(array('name' => 'Bob', 'description' => 'foobar'));
$person->setAge(25)->setGender("Male");
$person->save(); // returns true or false
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 BaseMongoRecord
{
    public function validatesName($name)
    {
        if ($name == 'Bob')
            return false;
        else
            return true;
    }
}
$person = new 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 BaseMongoRecord
{
    public function beforeSave()
    {
         if ($this->getName() == 'Bob')
             $this->setName('Bill');
    }
}

Differences between MongoRecord 1.x and 2.x

In 2.x the find() method is lazy and returns a MongoRecordIterator instead of a MongoRecord. The Iterator can be treated like an array and implements the PHP Iterator and Countable interfaces.

Because of this, 2.x is not backwards compatible with 1.x, since the result of find() is not guaranteed to behave the same. (e.g. $results[3] does not work with @find() in 2.×.) You may use findAll() instead, which exhibits the same behavior as find() in 1.×.

Credits

Thanks to Rasmus for the suggestion for lazy find().

mongorecord's People

Contributors

hyusetiawan avatar lubomir-haralampiev avatar lunaru avatar macedd avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.