File tree Expand file tree Collapse file tree 4 files changed +73
-2
lines changed Expand file tree Collapse file tree 4 files changed +73
-2
lines changed Original file line number Diff line number Diff line change @@ -10,7 +10,7 @@ Install using composer.
1010``` json
1111{
1212 "require" : {
13- "tarantool/mapper" : " 2.0.2 "
13+ "tarantool/mapper" : " 2.2.0 "
1414 }
1515}
1616```
@@ -98,10 +98,34 @@ $person->createIndex([
9898$person->createIndex([
9999 'fields' => ['name', 'birthday'],
100100 'type' => 'hash',
101- 'name' =>
101+ 'name' => 'name_with_birthday',
102102]);
103103```
104104
105+ # Use migrations
106+
107+ ``` php
108+ use Tarantool\Mapper\Mapper;
109+ use Tarantool\Mapper\Migration;
110+
111+ class InitTesterSchema implements Migration
112+ {
113+ public function migrate(Mapper $mapper)
114+ {
115+ $tester = $mapper->getSchema()->createSpace('tester');
116+ $tester->addProperty('id', 'unsigned');
117+ $tester->addProperty('name', 'str');
118+ $tester->createIndex('id');
119+ }
120+ }
121+
122+ $mapper->getBootstrap()->register(InitTesterSchema::class);
123+ // or register instance $mapper->getBootstrap()->register(new InitTesterSchema());
124+
125+ $mapper->getBootstrap()->migrate();
126+
127+ ```
128+
105129# Working with the data
106130Now you can store and retreive data from tarantool storage using mapper instance.
107131``` php
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Tarantool \Mapper ;
4+
5+ class Bootstrap
6+ {
7+ private $ mapper ;
8+ private $ migrations = [];
9+
10+ public function __construct (Mapper $ mapper )
11+ {
12+ $ this ->mapper = $ mapper ;
13+ }
14+
15+ public function register ($ instance )
16+ {
17+ $ this ->migrations [] = $ instance ;
18+ }
19+
20+ public function migrate ()
21+ {
22+ foreach ($ this ->migrations as $ migration ) {
23+ if (!is_object ($ migration )) {
24+ $ migration = new $ migration ;
25+ }
26+ $ migration ->migrate ($ this ->mapper );
27+ }
28+ }
29+ }
Original file line number Diff line number Diff line change @@ -10,6 +10,7 @@ class Mapper
1010 private $ client ;
1111 private $ plugins = [];
1212 private $ schema ;
13+ private $ bootstrap ;
1314
1415 public function __construct (Client $ client )
1516 {
@@ -56,6 +57,15 @@ public function findRepository(Entity $instance)
5657 throw new Exception ("No Repository for given Entity " );
5758 }
5859
60+ public function getBootstrap ()
61+ {
62+ if (!$ this ->bootstrap ) {
63+ $ this ->bootstrap = new Bootstrap ($ this );
64+ }
65+
66+ return $ this ->bootstrap ;
67+ }
68+
5969 public function getClient ()
6070 {
6171 return $ this ->client ;
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Tarantool \Mapper ;
4+
5+ interface Migration
6+ {
7+ public function migrate (Mapper $ mapper );
8+ }
You can’t perform that action at this time.
0 commit comments