PHPackages                             brandonlamb/spot - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. brandonlamb/spot

ActiveLibrary[Database &amp; ORM](/categories/database)

brandonlamb/spot
================

DataMapper ORM for PHP 5.5+

v1.0.17(11y ago)12.3k2BSDCPHP &gt;=5.5.0

Since Dec 6Pushed 11y ago2 watchersCompare

[ Source](https://github.com/brandonlamb/Spot)[ Packagist](https://packagist.org/packages/brandonlamb/spot)[ Docs](https://github.com/brandonlamb/Spot)[ RSS](/packages/brandonlamb-spot/feed)WikiDiscussions master Synced 3d ago

READMEChangelog (3)DependenciesVersions (16)Used By (0)

Spot PHP ORM+ODM
----------------

[](#spot-php-ormodm)

For Relational Databases and MongoDB

[![Build Status](https://camo.githubusercontent.com/f4805ce071e300ee01332af2b42632267c18cc0fed613b78af842e7e9a2e400d/68747470733a2f2f7777772e7472617669732d63692e6f72672f6272616e646f6e6c616d622f53706f742e706e673f6272616e63683d6d6173746572)](https://www.travis-ci.org/brandonlamb/Spot)

Connecting to a Database
========================

[](#connecting-to-a-database)

The `Spot\Config` object stores and references database connections by name. Create a new instance of `Spot\Config` and add database connections created outside of Spot. This was a change to allow your app to create the raw PDO connection and just reuse this. A big complaint I have always had is pretty much every ORM/Model class always wants to create this for you instead of being passed this connection.

```
// PostgreSQL
$db = new Pdo('pgsql:host=localhost;dbname=jdoe', 'jdoe', 'mypass');

$cfg = \Spot\Config::getInstance();
$adapter = $cfg->addConnection('db', new \Spot\Adapter\Pgsql($db));

$adapter = $cfg->connection('db');

```

Accessing the Mapper
====================

[](#accessing-the-mapper)

Since Spot follows the DataMapper design pattern, you will need a mapper instance for working with object Entities and database tables.

```
$mapper = new \Spot\Mapper($cfg);

```

Since you have to have access to your mapper anywhere you use the database, most people create a helper method to create a mapper instance once and then return the same instance when required again. Such a helper method might look something like this:

```
function get_mapper() {
    static $mapper;
    if($mapper === null) {
        $mapper = new \Spot\Mapper($cfg);
    }
    return $mapper;
}

```

Or if you have a Registry class in your framework:

```
$registry = Registry::getInstance();
$registry->set('mapper', $mapper);

$mapper = Register::get('mapper');

```

Or using a Dependency Injection Container

```
$di->setShared('mapper', $mapper);

$mapper = $di->getShared('mapper');

```

Creating Entities
=================

[](#creating-entities)

Entity classes can be named and namespaced however you want to set them up within your project structure. For the following examples, the Entities will just be prefixed with an `Entity` namespace for easy psr-0 compliant autoloading.

```
namespace Entity;

class Post extends \Spot\Entity
{
    protected static $datasource = 'posts';

    public static function fields()
    {
        return array(
            'id' => array('type' => 'int', 'primary' => true, 'serial' => true),
            'title' => array('type' => 'string', 'required' => true),
            'body' => array('type' => 'text', 'required' => true),
            'status' => array('type' => 'int', 'default' => 0, 'index' => true),
            'date_created' => array('type' => 'datetime')
        );
    }

    public static function relations()
    {
        return array(
            // Each post entity 'hasMany' comment entites
            'comments' => array(
                'type' => 'HasMany',
                'entity' => 'Entity_Post_Comment',
                'where' => array('post_id' => ':entity.id'),
                'order' => array('date_created' => 'ASC')
            )
        );
    }
}

```

Another entity example of a model class inside an application's Model namespace. This is the simplest definition, only defining the model's fields.

```
