Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
A data mapper library with a minimal impact on your domain logic. For PHP 5.3+
PHP Ruby
branch: develop

This branch is 12 commits ahead, 2 commits behind master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
guide @ def96d4

The cure

A data mapper library with a minimal impact on your domain logic. In plainer terms, a library for writing the models of your PHP application, mapping them to data stores such as MongoDB and unit testing against mock data stores.

This library came about from a number of previous interations including beautiful/domain and the frustrations of unit testing the active record pattern.

Written in PHP and must be used with 5.3 or later. The tests require the Kohana 3.3 framework for autoloading classes and configuration management. Kohana is included as a submodule.


The cure is available as a composer package so you can add it as a dependency.

    "require": {
        "php": ">=5.3.2",
        "the-cure/the-cure": ">=0.2.1"

Alternatively you can download the latest zip from github:

The cure v0.2.1


Before we go into that CRUD stuff let's explain a few ingredients of the cure.


In the cure we use a class known as a model to describe domain logic. Your models in your application will represent users, blog posts, spaceships or anything that concerns yours or your bosses business.

The cure provides two base models. Here is an example of the second, more ORM like model, TheCure\Models\Magic:

namespace Models;

use TheCure\Lists\AttributeList;

use TheCure\Attributes\Field;

use TheCure\Relationships\HasManyRelationship;

class User extends \TheCure\Models\MagicModel {

    public static function attributes()
        return new AttributeList(
            new Field('name'),
            new HasManyRelationship(
                array('mapperSuffix' => 'User')));


The magical ORM features of the class Models\User extends include providing a getter/setter method for each field along with several more per relationship. The example above includes a TheCure\Relationships\HasMany which adds methods for finding, adding and removing other User models from it's friends collection.


The second piece to the puzzle are mappers. These classes describe the behaviour for persisting your models to databases, flat files, JSON, S3, or whatever. Currently the cure provides two mappers, one for MongoDB, the other a mock mapper for your unit tests.

Because TheCure\Mappers\Mongo contains enough behaviour to get us going we can simply extend this class and leave it blank for our example below.

namespace Mappers\Mongo;

class User extends \TheCure\Mappers\MongoMapper {}


Now that we've assembled some ingredients let's go over the instructions for using them.

We create a container which is responsible for creating mapper objects that connect with a MongoDB.

$container = new TheCure\Container('Mongo');

We grab the user mapper, this will be used for creating a new user model as well as storing it later.

$mapper = $container->mapper('User'); // => Mappers\Mongo\User

And a new user model to represent a single person.

$user = $mapper->model(); // => Models\User

Set the model's name to "Luke", because it's a good name.


And persist it to MongoDB. As you can see the API is fairly minimal and quite self explanatory.


Find the same model using ::find_one().

$userCopy = $mapper->findOne(array('name' => 'Luke'));

// Note that these are the exact same object
var_dump($user === $userCopy);

Create another user with a name of "Jake".

$friend = $mapper->model();

Add Jake as a friend of Luke's.


Get Luke's friends as a collection.

foreach ($user->friends() as $_friend)
    echo "{$_friend->name()}\n";

For more information on the cure start with "General usage" and work your way from there.

And relax! The API is minimal so there is not much to learn.


  • 1717 non-commented lines of code
  • 28 classes with 127 methods (4 per class)
  • 7 interfaces
  • 705 statements (5 per method) with 100% covered
  • 127 tests and 165 asserts (in 0.97 seconds)

Run rake test to produce these stats yourself. We use a Rakefile for producing stats from our unit tests so you will need ruby and rake installed on your system.


Build Status

The cure is well covered with tests. We aim to:

  • test every individual unit in isolation via specification unit tests
  • maintain close to 100% test coverage, with 100% coverage during a release
  • test use cases via acceptance tests

Running tests

To run everything in standard PHPUnit mode:


To run a particular area of logic:

phpunit --group relationships

To run just the acceptance tests or specs:

phpunit --group acceptance
phpunit --group specs

To run a special spec report with code coverage:

rake test


MIT licensed.


The Gignite Team and anyone willing to contribute.

Gignite are hiring!

If you like working on open source projects like this as well as the opportunity to build Gignite with us then get in touch!

Something went wrong with that request. Please try again.