1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
require_once __DIR__ . '/../vendor/autoload.php'; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* This example will guide you through working with collection classes. |
6
|
|
|
* You will learn: |
7
|
|
|
* - how to locally map/reduce data |
8
|
|
|
* - how to work with low memory footprint |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
use Igni\Storage\Driver\Pdo\Connection; |
12
|
|
|
use Igni\Storage\Driver\Pdo\ConnectionOptions; |
13
|
|
|
use Igni\Storage\Driver\Pdo\Repository; |
14
|
|
|
use Igni\Storage\Storable; |
15
|
|
|
use Igni\Storage\Id; |
16
|
|
|
use Igni\Storage\Id\GenericId; |
17
|
|
|
use Igni\Storage\Mapping\Annotation\Entity; |
18
|
|
|
use Igni\Storage\Mapping\Annotation\Property; |
19
|
|
|
use Igni\Storage\Mapping\MappingStrategy; |
20
|
|
|
use Igni\Storage\Mapping\Type; |
21
|
|
|
use Igni\Storage\Storage; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* This is our example entity class - nothing fancy. |
25
|
|
|
* All properties are public to simplify the example. |
26
|
|
|
* All items are taken from tracks table. |
27
|
|
|
* @Entity(source="tracks") |
28
|
|
|
*/ |
29
|
|
|
class Track implements Storable |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @Property(type="id", name="TrackId", class=GenericId::class) |
33
|
|
|
*/ |
34
|
|
|
public $id; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @Property(type="string", name="Name") |
38
|
|
|
*/ |
39
|
|
|
protected $name; |
40
|
|
|
|
41
|
|
|
public function getId(): Id |
42
|
|
|
{ |
43
|
|
|
return $this->id; |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
|
48
|
|
|
$sqlLiteConnection = new Connection(__DIR__ . '/db.db', new ConnectionOptions('sqlite')); |
49
|
|
|
$sqlLiteConnection->open(); |
50
|
|
|
$unitOfWork = new Storage(); |
51
|
|
|
$unitOfWork->addRepository(new class($sqlLiteConnection, $unitOfWork->getEntityManager()) extends Repository { |
|
|
|
|
52
|
|
|
public function getEntityClass(): string |
53
|
|
|
{ |
54
|
|
|
return Track::class; |
55
|
|
|
} |
56
|
|
|
}); |
57
|
|
|
|
58
|
|
|
$track = $unitOfWork->get(Track::class, 1); |
59
|
|
|
|
60
|
|
|
$track->getComposer();// Instance of composer. |
|
|
|
|
61
|
|
|
|
62
|
|
|
|