1. Go to this page and download the library: Download egorov/mapper 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/ */
egorov / mapper example snippets
use Tarantool\Client\Client;
use Tarantool\Client\Connection\StreamConnection;
use Tarantool\Client\Packer\PurePacker;
use Tarantool\Mapper\Mapper;
$connection = new StreamConnection();
$client = new Client($connection, new PurePacker());
$mapper = new Mapper($client);
use Tarantool\Client\Connection\StreamConnection;
use Tarantool\Client\Packer\PurePacker;
use Tarantool\Mapper\Mapper;
use Tarantool\Mapper\Client;
$connection = new StreamConnection();
$client = new Client($connection, new PurePacker());
$mapper = new Mapper($client);
$result = $client->ping();
$log = $client->getLog();
$globalSpace = $mapper->find('_space', ['name' => '_space']);
echo $globalSpace->id; // 280
$indexes = $mapper->find('_index', ['id' => $globalSpace->id]);
var_dump($indexes); // indexes on _index space
echo $indexes[0]->name; // primary index
echo $indexes[0]->type; // tree
$guest = $mapper->find('_user', ['name' => 'guest']);
echo $guest->id; // 0
echo $guest->type; // user
$person = $mapper->getSchema()->createSpace('person');
// add properties
$person->addProperty('id', 'unsigned');
$person->addProperty('name', 'string');
$person->addProperty('birthday', 'unsigned');
$person->addProperty('gender', 'string');
// add multiple properties
$person->addProperties([
'telegram' => 'string',
'vk' => 'string',
'facebook' => 'string',
]);
// add indexes
// first index is primary
$person->createIndex([
'type' => 'hash', // define type
'fields' => ['id'],
]);
// create unique indexes using property or array of properties as parameter
$person->createIndex('name');
// create not unique indexes
$person->createIndex([
'fields' => 'birthday',
'unique' => false
]);
// if you wish - you can specify index name
$person->createIndex([
'fields' => ['name', 'birthday'],
'type' => 'hash',
'name' => 'name_with_birthday',
]);
use Tarantool\Mapper\Mapper;
use Tarantool\Mapper\Migration;
class InitTesterSchema implements Migration
{
public function migrate(Mapper $mapper)
{
$tester = $mapper->getSchema()->createSpace('tester', [
'engine' => 'memtx', // or vinyl
'properties' => [
'id' => 'unsigned',
'name' => 'string',
]
]);
$tester->createIndex('id');
}
}
$mapper->getBootstrap()->register(InitTesterSchema::class);
// or register instance $mapper->getBootstrap()->register(new InitTesterSchema());
$mapper->getBootstrap()->migrate();
use Tarantool\Mapper\Mapper;
use Tarantool\Mapper\Migration;
class InitTesterSchema implements Migration
{
public function migrate(Mapper $mapper)
{
$mapper->getSchema()->createSpace('person')
->addProperty('id', 'unsigned')
->addProperty('name', 'string')
->addIndex('id');
}
}
// get repository instance
$persons = $mapper->getRepository('person');
// create new entity
$dmitry = $persons->create([
'id' => 1,
'name' => 'Dmitry'
]);
// save
$mapper->save($dmitry);
// you can create entities using mapper wrapper.
// this way entity will be created and saved in the tarantool
$vasily = $mapper->create('person', [
'id' => 2,
'name' => 'Vasily'
]);
// you can retreive entites by id from repository
$helloWorld = $mapper->getRepository('post')->find(3);
// or using mapper wrapper
$helloWorld = $mapper->find('post', 3);
// updates are easy
$helloWorld->title = "Hello World!";
$mapper->save($helloWorld);
$note = $mapper->getSchema()->createSpace('note');
$note->addProperty('slug', 'string');
$note->addProperty('title', 'string',
$note->addProperty('status', 'string');
$note->addIndex('slug');
$note->addIndex([
'fields' => 'status',
'unique' => false
]);
// find using repository
$mapper->getRepository('note')->find(['status' => 'active']);
// find using shortcut
$mapper->find('note', ['status' => 'active']);
// find first
$mapper->getRepository('note')->findOne(['slug' => 'my-secret-note']);
// composite indexes can be used partial
$person = $mapper->getSchema()->createSpace('person');
$person->addProperty('id', 'unsigned');
$person->addProperty('client', 'unsigned');
$person->addProperty('sector', 'unsigned');
$person->addProperty('name', 'unsigned');
$person->addIndex('id');
$person->addIndex([
'fields' => ['client', 'sector'],
'unique' => false
]);
// using index parts
$mapper->find('person', ['client' => 2]);
$mapper->find('person', ['client' => 2, 'sector' => 27]);
$mapper->getSchema()->createSpace('post', [
'id' => 'unsigned',
'title' => 'string',
'body' => 'string',
])
->addIndex('id');
$mapper->getPlugin(Tarantool\Mapper\Plugin\Sequence::class);
$entity = $mapper->create('post', [
'title' => 'Autoincrement implemented',
'body' => 'You can use Sequence plugin to track and fill your entity id'
]);
echo $entity->id; // will be set when you create an instance
$userClasses = $mapper->getPlugin(Tarantool\Mapper\Plugin\UserClasses::class);
$userClasses->mapEntity('person', Application\Entity\Person::class);
$userClasses->mapRepository('person', Application\Repository\Person::class);
$nekufa = $mapper->create('person', [
'email' => '[email protected]'
]);
get_class($nekufa); // Application\Entity\Person;
$mapper->getSchema()->getSpace('person')->getRepository(); // will be instance of Application\Repository\Person
namespace Entity;
use Tarantool\Mapper\Entity;
class Person extends Entity
{
/**
* @var integer
*/
public $id;
/**
* @var string
*/
public $name;
}
class Post extends Entity
{
/**
* @var integer
*/
public $id;
/**
* @var string
*/
public $slug;
/**
* @var string
*/
public $title;
/**
* @var string
*/
public $body;
/**
* @var Person
*/
public $author;
/**
* @var integer
* @
namespace Repository;
use Tarantool\Mapper\Repository;
class Post extends Repository
{
public $engine = 'memtx'; // or vinyl
public $indexes = [
// if your index is unique, you can set property collection
['id'],
// extended definition unique index with one field
[
'fields' => ['slug'],
'unique' => true,
],
// extended definition (similar to Space::addIndex params)
// [
// 'fields' => ['year', 'month', 'day'],
// 'unique' => true
// ],
];
}
$mapper->getPlugin(Tarantool\Mapper\Plugin\Sequence::class); // just not to fill id manually
$mapper->getPlugin(Tarantool\Mapper\Plugin\Annotation::class)
->register(Entity\Person::class)
->register(Entity\Post::class)
->register(Repository\Person::class)
->migrate(); // sync database schema with code
$nekufa = $mapper->create('person', ['name' => 'dmitry']);
$post = $mapper->create('post', [
'author' => $nekufa,
'slug' => 'hello-world',
'title' => 'Hello world',
'body' => 'Now you can use mapper better way'
]);
// in addition you can simple get related entity
$post->getAuthor() == $nekufa; // true
// or related collection
$nekufa->getPostCollection() == [$post]; // true
$dmitry = $mapper->getRepository('person')->findOne(['name' => 'Dmitry']); // person with id 1
echo $dmitry == $mapper->findOne('person', 1); // true
// query result are cached until you create new entites
$mapper->getRepository('person')->findOne(['name' => 'Dmitry']);
// you can flush cache manually
$mapper->getRepository('person')->flushCache();
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.