1. Go to this page and download the library: Download vlucas/spot2 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/ */
vlucas / spot2 example snippets
$cfg = new \Spot\Config();
// MySQL
$cfg->addConnection('mysql', 'mysql://user:password@localhost/database_name');
// Sqlite
$cfg->addConnection('sqlite', 'sqlite://path/to/database.sqlite');
$spot = new \Spot\Locator($cfg);
function spot() {
static $spot;
if($spot === null) {
$spot = new \Spot\Locator();
$spot->config()->addConnection('test_mysql', 'mysql://user:password@localhost/database_name');
}
return $spot;
}
$postMapper = $spot->mapper('Entity\Post');
namespace Entity;
use Spot\EntityInterface as Entity;
use Spot\MapperInterface as Mapper;
class Post extends \Spot\Entity
{
protected static $table = 'posts';
public static function fields()
{
return [
'id' => ['type' => 'integer', 'autoincrement' => true, 'primary' => true],
'title' => ['type' => 'string', 'mapper, Entity $entity)
{
return [
'tags' => $mapper->hasManyThrough($entity, 'Entity\Tag', 'Entity\PostTag', 'tag_id', 'post_id'),
'comments' => $mapper->hasMany($entity, 'Entity\Post\Comment', 'post_id')->order(['date_created' => 'ASC']),
'author' => $mapper->belongsTo($entity, 'Entity\Author', 'author_id')
];
}
}
namespace Entity;
class Post extends \Spot\Entity
{
protected static $mapper = 'Entity\Mapper\Post';
// ... snip ...
}
namespace Entity\Mapper;
use Spot\Mapper;
class Post extends Mapper
{
/**
* Get 10 most recent posts for display on the sidebar
*
* @return \Spot\Query
*/
public function mostRecentPostsForSidebar()
{
return $this->where(['status' => 'active'])
->order(['date_created' => 'DESC'])
->limit(10);
}
}
// Where can be called directly from the mapper
$posts = $mapper->where(['status' => 1]);
// Or chained using the returned `Spot\Query` object - results identical to above
$posts = $mapper->all()->where(['status' => 1]);
// Or more explicitly using using `select`, which always returns a `Spot\Query` object
$posts = $mapper->select()->where(['status' => 1]);
# All posts with a 'published' status, descending by date_created
$posts = $mapper->all()
->where(['status' => 'published'])
->order(['date_created' => 'DESC']);
# All posts that are not published
$posts = $mapper->all()
->where(['status <>' => 'published'])
# All posts created before 3 days ago
$posts = $mapper->all()
->where(['date_created <' => new \DateTime('-3 days')]);
# Posts with 'id' of 1, 2, 5, 12, or 15 - Array value = automatic "IN" clause
$posts = $mapper->all()
->where(['id' => [1, 2, 5, 12, 15]]);
$posts = $mapper->query("SELECT * FROM posts WHERE id = 1");
$posts = $mapper->query("SELECT * FROM posts WHERE id = ?", [1]);
$posts = $mapper->query("SELECT * FROM posts WHERE id = :id", ['id' => 1]);