|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
/** |
|
3
|
|
|
* Simple tutorial step by step introducing user to world of storage framework. |
|
4
|
|
|
* Each section contains comment explaining what is happening behind the scenes. |
|
5
|
|
|
*/ |
|
6
|
|
|
require_once __DIR__ . '/../vendor/autoload.php'; |
|
7
|
|
|
|
|
8
|
|
|
use Igni\Storage\Mapping\Annotation\Entity; |
|
9
|
|
|
use Igni\Storage\Mapping\Annotation\Property; |
|
10
|
|
|
use Igni\Storage\Id\AutoGenerateId; |
|
11
|
|
|
use Igni\Storage\Driver\Pdo\Repository; |
|
12
|
|
|
use Igni\Storage\Driver\Pdo\Connection; |
|
13
|
|
|
use Igni\Storage\Driver\Pdo\ConnectionOptions; |
|
14
|
|
|
use Igni\Storage\Storage; |
|
15
|
|
|
use Igni\Storage\Mapping\Collection\Collection; |
|
16
|
|
|
use Igni\Storage\Id\GenericId; |
|
17
|
|
|
use Igni\Storage\Storable; |
|
18
|
|
|
|
|
19
|
|
|
// Below there are entities used in the example: |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Records are taken from `tracks` table and hydrated to `Track` instance |
|
23
|
|
|
* @Entity(source="tracks") |
|
24
|
|
|
*/ |
|
25
|
|
|
class Track implements Storable |
|
26
|
|
|
{ |
|
27
|
|
|
use AutoGenerateId; |
|
28
|
|
|
/** @Property\Id(class=GenericId::class, name="TrackId") */ |
|
29
|
|
|
public $id; |
|
30
|
|
|
/** @Property\Text(name="Name") */ |
|
31
|
|
|
public $title; |
|
32
|
|
|
/** @Property\Reference(ArtistEntity::class, name="ArtistId", readonly=true) */ |
|
33
|
|
|
public $artist; |
|
34
|
|
|
|
|
35
|
|
|
public function __construct(Artist $artist, string $title) |
|
36
|
|
|
{ |
|
37
|
|
|
$this->title = $title; |
|
38
|
|
|
$this->artist = $artist; |
|
39
|
|
|
} |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Records are taken from `artists` table and hydrated to `Artist` instance |
|
44
|
|
|
* @Entity(source="artists") |
|
45
|
|
|
*/ |
|
46
|
|
|
class Artist implements Storable |
|
47
|
|
|
{ |
|
48
|
|
|
use AutoGenerateId; |
|
49
|
|
|
|
|
50
|
|
|
/** @Property\Id(class=GenericId::class, name="ArtistId") */ |
|
51
|
|
|
public $id; |
|
52
|
|
|
|
|
53
|
|
|
/** @Property\Text() */ |
|
54
|
|
|
public $name; |
|
55
|
|
|
|
|
56
|
|
|
public function __construct(string $name) |
|
57
|
|
|
{ |
|
58
|
|
|
$this->name = $name; |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
// The following lines contain repositories |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Repositories can be used to define complex queries and aggregations by using native SQL queries. |
|
66
|
|
|
*/ |
|
67
|
|
|
class TrackRepository extends Repository |
|
68
|
|
|
{ |
|
69
|
|
|
/** |
|
70
|
|
|
* Finds all tracks that belongs to given artist and return results as collection. |
|
71
|
|
|
*/ |
|
72
|
|
|
public function findByArtist(Artist $artist): Collection |
|
73
|
|
|
{ |
|
74
|
|
|
$cursor = $this->query(" |
|
75
|
|
|
SELECT tracks.* |
|
76
|
|
|
FROM tracks |
|
77
|
|
|
JOIN albums ON albums.AlbumId = tracks.AlbumId |
|
78
|
|
|
JOIN artists ON artists.ArtistId = albums.ArtistId |
|
79
|
|
|
WHERE albums.ArtistId = :id |
|
80
|
|
|
", [ |
|
81
|
|
|
'id' => $artist->getId() |
|
82
|
|
|
]); |
|
83
|
|
|
|
|
84
|
|
|
return new Collection($cursor); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public static function getEntityClass(): string |
|
88
|
|
|
{ |
|
89
|
|
|
return Track::class; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
// Work with unit of work |
|
93
|
|
|
// Define connections: |
|
94
|
|
|
$sqliteConnection = new Connection(__DIR__ . '/db.db'); |
|
95
|
|
|
$storage = new Storage(); |
|
96
|
|
|
|
|
97
|
|
|
// Attach repositories |
|
98
|
|
|
$storage->addRepository( |
|
99
|
|
|
// Dynamic Repository |
|
100
|
|
|
new class($sqliteConnection, $storage->getEntityManager()) extends Repository { |
|
|
|
|
|
|
101
|
|
|
public static function getEntityClass(): string |
|
102
|
|
|
{ |
|
103
|
|
|
return Artist::class; |
|
104
|
|
|
} |
|
105
|
|
|
}, |
|
106
|
|
|
|
|
107
|
|
|
// Custom Repository class |
|
108
|
|
|
new TrackRepository($sqliteConnection, $storage->getEntityManager()) |
|
|
|
|
|
|
109
|
|
|
|
|
110
|
|
|
); |
|
111
|
|
|
|
|
112
|
|
|
// Fetch items from database |
|
113
|
|
|
|
|
114
|
|
|
$artist = $storage->get(Artist::class, 1); // This is equivalent to: SELECT *FROM artists WHERE ArtistId = 1 |
|
115
|
|
|
$track = $storage->get(Track::class, 1); // This is equivalent to: SELECT *FROM tracks WHERE TrackId = 1 |
|
116
|
|
|
|
|
117
|
|
|
// Iterate through all tracks that belong to given artist |
|
118
|
|
|
foreach ($storage->getRepository(Track::class)->findByArtist($artist) as $track) { |
|
|
|
|
|
|
119
|
|
|
echo $track->title; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
// Create new artist. |
|
123
|
|
|
$jimmy = new Artist('Moaning Jimmy'); |
|
124
|
|
|
$storage->persist($jimmy); |
|
125
|
|
|
|
|
126
|
|
|
// Update track's artist. |
|
127
|
|
|
$track->artist = $jimmy; |
|
128
|
|
|
|
|
129
|
|
|
$storage->remove($artist); // This will remove existing artist with id 1 once commit is executed. |
|
130
|
|
|
|
|
131
|
|
|
$storage->persist($track); // Save changes that will be flushed to database once commit is executed. |
|
132
|
|
|
|
|
133
|
|
|
$storage->commit(); // All update queries will happen from this point on |
|
134
|
|
|
|