PHP code example of brandonlamb / spot

1. Go to this page and download the library: Download brandonlamb/spot library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

brandonlamb / spot example snippets


// 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');

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

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', '           // 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')
            )
        );
    }
}

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

// 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>";
}