Skip to content

Commit 26b16ee

Browse files
authored
update readme file
1 parent 23f31a4 commit 26b16ee

File tree

1 file changed

+188
-0
lines changed

1 file changed

+188
-0
lines changed

README.md

Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,191 @@
66
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/tarantool-php/mapper/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/tarantool-php/mapper/?branch=master)
77
[![Code Coverage](https://scrutinizer-ci.com/g/tarantool-php/mapper/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/tarantool-php/mapper/?branch=master)
88

9+
Install using composer.
10+
```json
11+
{
12+
"require": {
13+
"tarantool/mapper": "^2.0.0"
14+
}
15+
}
16+
```
17+
18+
# Instantiate manager
19+
Usually, you manage dependencies in your service provider.
20+
To get started you should instantiate connection, packer, client and manager itself.
21+
In this example we use PurePacker and StreamConnection. It means you don't need any pecl extensions. To see other implementations please check [client documentation](https://github.com/tarantool-php/client#usage)
22+
23+
```php
24+
use Tarantool\Client\Client;
25+
use Tarantool\Client\Connection\StreamConnection;
26+
use Tarantool\Client\Packer\PurePacker;
27+
use Tarantool\Mapper\Manager;
28+
29+
$connection = new StreamConnection();
30+
$client = new Client($connection, new PurePacker());
31+
$mapper = new Mapper($client);
32+
```
33+
34+
# Logging
35+
By default, client does not logs tarantool requests, you can use mapper\client that supports logging.
36+
```php
37+
use Tarantool\Client\Connection\StreamConnection;
38+
use Tarantool\Client\Packer\PurePacker;
39+
use Tarantool\Mapper\Manager;
40+
use Tarantool\Mapper\Client;
41+
42+
$connection = new StreamConnection();
43+
$client = new Client($connection, new PurePacker());
44+
$mapper = new Mapper($client);
45+
46+
$result = $client->ping();
47+
48+
$log = $client->getLog();
49+
```
50+
51+
# Describe entities
52+
To get started you should describe your types and fields using meta object.
53+
```php
54+
55+
$person = $mapper->getSchema()->createSpace('person');
56+
57+
// add properties
58+
$person->addProperty('id', 'unsigned');
59+
$person->addProperty('name', 'str');
60+
$person->addProperty('birthday', 'unsigned');
61+
$person->addProperty('gender', 'str');
62+
63+
// add indexes
64+
// first index is primary
65+
$person->createIndex([
66+
'type' => 'hash', // define type
67+
'fields' => ['id'],
68+
]);
69+
70+
// create unique indexes using property or array of properties as parameter
71+
$person->createIndex('name');
72+
73+
// create not unique indexes
74+
$person->createIndex([
75+
'fields' => 'birthday',
76+
'unique' => false
77+
]);
78+
79+
// if you wish - you can specify index name
80+
$person->createIndex([
81+
'fields' => ['name', 'birthday'],
82+
'type' => 'hash',
83+
'name' =>
84+
]);
85+
```
86+
87+
# Working with the data
88+
Now you can store and retreive data from tarantool storage using mapper instance.
89+
```php
90+
// get repository instance
91+
$persons = $mapper->getRepository('person');
92+
93+
// create new entity
94+
$dmitry = $persons->create([
95+
'id' => 1,
96+
'name' => 'Dmitry'
97+
]);
98+
99+
// save
100+
$mapper->save($dmitry);
101+
102+
// you can create entities using manager wrapper.
103+
// this way entity will be created and saved in the tarantool
104+
$vasily = $mapper->create('person', [
105+
'id' => 2,
106+
'name' => 'Vasily'
107+
]);
108+
109+
// you can retreive entites by id from repository
110+
$helloWorld = $mapper->getRepository('post')->find(3);
111+
112+
// or using manager wrapper
113+
$helloWorld = $mapper->find('post', 3);
114+
115+
// updates are easy
116+
$helloWorld->title = "Hello World!";
117+
$mapper->save($helloWorld);
118+
```
119+
# Indexes
120+
```php
121+
$note = $mapper->getSchema()->createSpace('note');
122+
$note->addProperty('slug', 'str');
123+
$note->addProperty('title', 'str',
124+
$note->addProperty('status', 'str');
125+
126+
$note->addIndex('slug');
127+
$note->addIndex([
128+
fields' => 'status',
129+
'unique' => false
130+
]);
131+
132+
// find using repository
133+
$mapper->getRepository('note')->find(['status' => 'active']);
134+
// find using shortcut
135+
$mapper->find('note', ['status' => 'active']);
136+
137+
// find first
138+
$mapper->getRepository('note')->findOne(['slug' => 'my-secret-note']);
139+
140+
// composite indexes can be used partial
141+
$person = $mapper->getSchema()->createSpace('person');
142+
$person->addProperty('id', 'unsigned');
143+
$person->addProperty('client', 'unsigned');
144+
$person->addProperty('sector', 'unsigned');
145+
$person->addProperty('name', 'unsigned');
146+
147+
$person->addIndex('id');
148+
$person->addIndex([
149+
'fields' => ['client', 'sector'],
150+
'unique' => false
151+
]);
152+
153+
// using index parts
154+
$mapper->find('person', ['client' => 2]);
155+
$mapper->find('person', ['client' => 2, 'sector' => 27]);
156+
```
157+
158+
# Array properties
159+
You can store arrays as property without any serialization to string.
160+
```php
161+
$pattern = $mapper->getSchema()->createSpace('shift_pattern');
162+
$pattern->addProperty('id', 'unsigned');
163+
$pattern->addProperty('title', 'str');
164+
$pattern->addProperty('pattern', '*');
165+
166+
$pattern->addIndex('id');
167+
168+
$mapper->create('shift_pattern', [
169+
'id' => 1,
170+
'title' => '5 days week',
171+
'pattern' => [
172+
['work' => true],
173+
['work' => true],
174+
['work' => true],
175+
['work' => true],
176+
['work' => true],
177+
['work' => false],
178+
['work' => false],
179+
]
180+
]);
181+
182+
$mapper->get('shift_pattern', 1)->pattern[5]; // read element with index 5 from pattern array
183+
```
184+
185+
# Internals
186+
Mapper uses IdentityMap and query caching
187+
```php
188+
$dmitry = $mapper->getRepository('person')->findOne(['name' => 'Dmitry']); // person with id 1
189+
echo $dmitry == $mapper->findOne('person', 1); // true
190+
191+
// query result are cached until you create new entites
192+
$mapper->getRepository('person')->findOne(['name' => 'Dmitry']);
193+
194+
// you can flush cache manually
195+
$mapper->getRepository('person')->flushCache();
196+
```

0 commit comments

Comments
 (0)