2017 © Pedro Peláez
 

library spot

DataMapper ORM for PHP 5.5+

image

brandonlamb/spot

DataMapper ORM for PHP 5.5+

  • Sunday, November 30, 2014
  • by brandonlamb
  • Repository
  • 2 Watchers
  • 1 Stars
  • 1,661 Installations
  • C
  • 0 Dependents
  • 0 Suggesters
  • 14 Forks
  • 0 Open issues
  • 16 Versions
  • 1 % Grown

The README.md

Spot PHP ORM+ODM

For Relational Databases and MongoDB, (*1)

Build Status, (*2)

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., (*3)

// 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

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

$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:, (*5)

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:, (*6)

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

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

Or using a Dependency Injection Container, (*7)

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

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

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., (*8)

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., (*9)

<?php
namespace Blog\Model;
use \Spot\Entity;

class Game extends Entity
{
    protected static $datasource = 'game';

    public static function fields()
    {
        return array(
            'id' => array('type' => 'int', 'primary' => true, 'serial' => true),
            'status_id' => array('type' => 'int', 'default' => 0, 'index' => true),
            'date_created' => array('type' => 'datetime', 'default' => date('Y-m-d h:m:i'), 'required' => true),
            'image_count' => array('type' => 'int', 'default' => 0, 'index' => true),
            'name' => array('type' => 'string', 'required' => true),
            'slug' => array('type' => 'string', 'required' => true),
        );
    }
}

Built-in Field Types

All the basic field types are built-in with all the default functionality provided for you:, (*10)

  • string
  • int
  • float/double/decimal
  • boolean
  • text
  • date
  • datetime
  • timestamp
  • year
  • month
  • day

Registering Custom Field Types

If you want to register your own custom field type with custom functionality on get/set, have a look at the clases in the Spot\Type namespace, make your own, and register it in Spot\Config:, (*11)

$this->setTypeHandler('string', '\Spot\Type\String');

Relation Types

Entity relation types are:, (*12)

  • HasOne
  • HasMany
  • HasManyThrough

Finders (Mapper)

The main finders used most are all to return a collection of entities, and first or get to return a single entity matching the conditions., (*13)

all(entityName, [conditions])

Find all entityName that matches the given conditions and return a Spot\Entity\Collection of loaded Spot\Entity objects., (*14)

// Conditions can be the second argument
$posts = $mapper->all('Entity\Post', array('status' => 1));

// Or chained using the returned `Spot\Query` object - results identical to above
$posts = $mapper->all('Entity\Post')->where(array('status' => 1));

// Or building up a query programmatically
$posts = $mapper->all('Entity\Post');
$posts->where(array('date_created :gt', date('Y-m-d'));

... // Do some checks

$posts->limit(10);

Since a Spot\Query object is returned, conditions and other statements can be chained in any way or order you want. The query will be lazy-executed on interation or count, or manually by ending the chain with a call to execute()., (*15)

first(entityName, [conditions])

Find and return a single Spot\Entity object that matches the criteria., (*16)

$post = $mapper->first('Entity\Post', array('title' => "Test Post"));

Iterating Over Results

// Fetch mapper from DI container
$mapper = $di->getShared('mapper');

// Get Query object to add constraints
$posts = $mapper->all('Entity\Posts');

// Find posts where the commenter's user_id is 123
$posts->where(array('user_id :eq', 123));

// Only get 10 results
$limit = (int) $_POST['limit'];
$posts->limit($limit);

// Loop over results
foreach ($posts as $post) {
    echo "Title: " . $post->title . "<br>";
    echo "Created: " . $post->date_created . "<br>";
}

The Versions

30/11 2014

dev-master

9999999-dev https://github.com/brandonlamb/Spot

DataMapper ORM for PHP 5.5+

  Sources   Download

BSD

The Requires

  • php >=5.5.0

 

orm database model mysql odm datamapper

30/11 2014

v1.0.17

1.0.17.0 https://github.com/brandonlamb/Spot

DataMapper ORM for PHP 5.5+

  Sources   Download

BSD

The Requires

  • php >=5.5.0

 

orm database model mysql odm datamapper

30/11 2014

dev-develop

dev-develop https://github.com/brandonlamb/Spot

DataMapper ORM for PHP 5.5+

  Sources   Download

BSD

The Requires

  • php >=5.5.0

 

orm database model mysql odm datamapper

30/11 2014

v1.0.16

1.0.16.0 https://github.com/brandonlamb/Spot

DataMapper ORM for PHP 5.5+

  Sources   Download

BSD

The Requires

  • php >=5.5.0

 

orm database model mysql odm datamapper

30/11 2014

dev-features/fetch-array

dev-features/fetch-array https://github.com/brandonlamb/Spot

DataMapper ORM for PHP 5.5+

  Sources   Download

BSD

The Requires

  • php >=5.5.0

 

orm database model mysql odm datamapper

24/02 2014

v1.0.15

1.0.15.0 https://github.com/brandonlamb/Spot

DataMapper ORM for PHP 5.5+

  Sources   Download

BSD

The Requires

  • php >=5.5.0

 

orm database model mysql odm datamapper

17/02 2014

1.0.14

1.0.14.0 https://github.com/brandonlamb/Spot

DataMapper ORM for PHP 5.5+

  Sources   Download

BSD

The Requires

  • php >=5.5.0

 

orm database model mysql odm datamapper

14/01 2014

1.0.13

1.0.13.0 https://github.com/brandonlamb/Spot

DataMapper ORM for PHP 5.5+

  Sources   Download

BSD

The Requires

  • php >=5.5.0

 

orm database model mysql odm datamapper

14/01 2014

1.0.12

1.0.12.0 https://github.com/brandonlamb/Spot

DataMapper ORM for PHP 5.5+

  Sources   Download

BSD

The Requires

  • php >=5.5.0

 

orm database model mysql odm datamapper

10/01 2014

1.0.11

1.0.11.0 https://github.com/brandonlamb/Spot

DataMapper ORM for PHP 5.5+

  Sources   Download

BSD

The Requires

  • php >=5.5.0

 

orm database model mysql odm datamapper

07/01 2014

1.0.10

1.0.10.0 https://github.com/brandonlamb/Spot

DataMapper ORM for PHP 5.5+

  Sources   Download

BSD

The Requires

  • php >=5.5.0

 

orm database model mysql odm datamapper

06/01 2014

1.0.9

1.0.9.0 https://github.com/brandonlamb/Spot

DataMapper ORM for PHP 5.5+

  Sources   Download

BSD

The Requires

  • php >=5.5.0

 

orm database model mysql odm datamapper

05/01 2014

1.0.8

1.0.8.0 https://github.com/brandonlamb/Spot

DataMapper ORM for PHP 5.5+

  Sources   Download

BSD

The Requires

  • php >=5.5.0

 

orm database model mysql odm datamapper

07/12 2013

1.0.4

1.0.4.0 https://github.com/brandonlamb/Spot

DataMapper ORM for PHP 5.5+

  Sources   Download

BSD

The Requires

  • php >=5.5.0

 

orm database model mysql odm datamapper

06/12 2013

1.0.3

1.0.3.0 https://github.com/brandonlamb/Spot

DataMapper ORM for PHP 5.5+

  Sources   Download

BSD

The Requires

  • php >=5.5.0

 

orm database model mysql odm datamapper

06/12 2013

1.0.2

1.0.2.0 https://github.com/brandonlamb/Spot

DataMapper ORM for PHP 5.5+

  Sources   Download

BSD

The Requires

  • php >=5.5.0

 

orm database model mysql odm datamapper