|
7 | 7 | [![Coverage Status][ico-coveralls]][link-coveralls] |
8 | 8 |
|
9 | 9 | Persistence layer for Hawkbit PSR-7 Micro PHP framework. |
10 | | -Hawkbit Persitence uses factories of `dasprid/container-interop-doctrine` and wraps them with in a PersistenceService |
| 10 | +Features unit of work, identity map, object graph, popo's and mapper. |
11 | 11 |
|
12 | 12 | ## Install |
13 | 13 |
|
14 | 14 | ### Using Composer |
15 | 15 |
|
16 | | -Hawkbit Persistence is available on [Packagist][link-packagist] and can be installed using [Composer](https://getcomposer.org/). This can be done by running the following command or by updating your `composer.json` file. |
| 16 | +Hawkbit Database is available on [Packagist][link-packagist] and can be installed using [Composer](https://getcomposer.org/). This can be done by running the following command or by updating your `composer.json` file. |
17 | 17 |
|
18 | 18 | ```bash |
19 | | -composer require hawkbit/persistence |
| 19 | +composer require hawkbit/database |
20 | 20 | ``` |
21 | 21 |
|
22 | 22 | composer.json |
23 | 23 |
|
24 | 24 | ```javascript |
25 | 25 | { |
26 | 26 | "require": { |
27 | | - "hawkbit/persistence": "~1.0" |
| 27 | + "hawkbit/database": "~1.0" |
28 | 28 | } |
29 | 29 | } |
30 | 30 | ``` |
@@ -52,77 +52,90 @@ The following versions of PHP are supported by this version. |
52 | 52 |
|
53 | 53 | ## Setup |
54 | 54 |
|
55 | | -Setup with an existing application configuration (we refer to [tests/assets/config.php](tests/assets/config.php)) |
| 55 | +Create a Mapper and an entity. See |
| 56 | + |
| 57 | +Create a Connection and register mappers |
56 | 58 |
|
57 | 59 | ```php |
58 | 60 | <?php |
59 | 61 |
|
60 | | -use \Hawkbit\Application; |
61 | | -use \Hawkbit\Persistence\PersistenceService; |
62 | | -use \Hawkbit\Persistence\PersistenceServiceProvider; |
| 62 | +use Hawkbit\Storage\ConnectionManager; |
| 63 | +use Application\Persistence\Mappers\PostMapper; |
63 | 64 |
|
64 | | -$app = new Application(require_once __DIR__ . '/config.php'); |
| 65 | +$connection = ConnectionManager::create([ |
| 66 | + 'url' => 'sqlite:///:memory:', |
| 67 | + 'memory' => 'true' |
| 68 | +]); |
65 | 69 |
|
66 | | -$entityFactoryClass = \ContainerInteropDoctrine\EntityManagerFactory::class; |
| 70 | +$connection->getMapperLocator()->register(PostMapper::class); |
| 71 | +``` |
67 | 72 |
|
68 | | -$persistenceService = new PersistenceService([ |
69 | | - PersistenceService::resolveFactoryAlias($entityFactoryClass) => [$entityFactoryClass] |
70 | | -], $app); |
| 73 | +Load Mapper by mapper class or entity class |
71 | 74 |
|
72 | | -$app->register(new PersistenceServiceProvider($persistenceService)); |
73 | | -``` |
| 75 | +```php |
| 76 | +<?php |
| 77 | + |
| 78 | +use Application\Persistence\Mappers\PostMapper; |
| 79 | +use Application\Persistence\Entities\Post; |
74 | 80 |
|
75 | | -## Examples |
| 81 | +// load by mapper |
| 82 | +$mapper = $connection->loadMapper(PostMapper::class); |
76 | 83 |
|
77 | | -### Full configuration |
| 84 | +// load by entity |
| 85 | +$mapper = $connection->loadMapper(Post::class); |
| 86 | + |
| 87 | +``` |
78 | 88 |
|
79 | | -A full configuration is available on [DASPRiD/container-interop-doctrine/example/full-config.php](https://github.com/DASPRiD/container-interop-doctrine/blob/master/example/full-config.php). |
80 | | -Refer to [container-interop-doctrine Documentation](https://github.com/DASPRiD/container-interop-doctrine) for further instructions on factories. |
| 89 | +## Data manipulation |
81 | 90 |
|
82 | | -### Persistence from Hawbit Application |
| 91 | +### Create entity |
83 | 92 |
|
84 | 93 | ```php |
85 | 94 | <?php |
86 | 95 |
|
87 | | -/** @var \Hawkbit\Persistence\PersistenceServiceInterface $persistence */ |
88 | | -$persistence = $app[\Hawkbit\Persistence\PersistenceServiceInterface::class]; |
| 96 | +use Application\Persistence\Entities\Post; |
89 | 97 |
|
90 | | -$em = $persistence->getEntityManager(); |
| 98 | +$entity = new Post(); |
91 | 99 |
|
92 | | -// or with from specific connection |
93 | | -$em = $persistence->getEntityManager('connectionname'); |
| 100 | +$entity->setContent('cnt'); |
| 101 | + |
| 102 | +/** @var Post $createdEntity */ |
| 103 | +$mapper->create($entity); |
94 | 104 |
|
95 | 105 | ``` |
96 | 106 |
|
97 | | -### Persistence in a Hawkbit controller |
98 | 107 |
|
99 | | -Access persistence service in controller. Hawbit is inject classes to controllers by default. |
| 108 | +### Load entity |
100 | 109 |
|
101 | 110 | ```php |
102 | 111 | <?php |
103 | 112 |
|
104 | | -use \Hawkbit\Persistence\PersistenceServiceInterface; |
105 | | - |
106 | | -class MyController{ |
107 | | - |
108 | | - /** |
109 | | - * @var \Hawkbit\Persistence\PersistenceServiceInterface |
110 | | - */ |
111 | | - private $persistence = null; |
112 | | - |
113 | | - public function __construct(PersistenceServiceInterface $persistence){ |
114 | | - $this->persistence = $persistence; |
115 | | - } |
116 | | - |
117 | | - public function index(){ |
118 | | - $em = $this->persistence->getEntityManager(); |
119 | | - |
120 | | - // or with from specific connection |
121 | | - $em = $this->persistence->getEntityManager('connectionname'); |
122 | | - } |
123 | | -} |
| 113 | +$entity = $mapper->find(['id' => 1]); |
| 114 | + |
124 | 115 | ``` |
125 | 116 |
|
| 117 | + |
| 118 | +### Update entity |
| 119 | + |
| 120 | +```php |
| 121 | +<?php |
| 122 | + |
| 123 | +$entity->setContent('FOO'); |
| 124 | +$mapper->update($entity); |
| 125 | + |
| 126 | +``` |
| 127 | + |
| 128 | +### Delete entity |
| 129 | + |
| 130 | +```php |
| 131 | +<?php |
| 132 | + |
| 133 | +$mapper->delete($entity); |
| 134 | + |
| 135 | +``` |
| 136 | + |
| 137 | +## Transactions |
| 138 | + |
126 | 139 | ## Change log |
127 | 140 |
|
128 | 141 | Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently. |
|
0 commit comments