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