Skip to content

Commit 305e48d

Browse files
author
Marco Bunge
committed
Update identity map and unit of work. Fix datatype issues
1 parent a6a0593 commit 305e48d

File tree

12 files changed

+453
-96
lines changed

12 files changed

+453
-96
lines changed

README.md

Lines changed: 60 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,24 @@
77
[![Coverage Status][ico-coveralls]][link-coveralls]
88

99
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.
1111

1212
## Install
1313

1414
### Using Composer
1515

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.
1717

1818
```bash
19-
composer require hawkbit/persistence
19+
composer require hawkbit/database
2020
```
2121

2222
composer.json
2323

2424
```javascript
2525
{
2626
"require": {
27-
"hawkbit/persistence": "~1.0"
27+
"hawkbit/database": "~1.0"
2828
}
2929
}
3030
```
@@ -52,77 +52,90 @@ The following versions of PHP are supported by this version.
5252

5353
## Setup
5454

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
5658

5759
```php
5860
<?php
5961

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;
6364

64-
$app = new Application(require_once __DIR__ . '/config.php');
65+
$connection = ConnectionManager::create([
66+
'url' => 'sqlite:///:memory:',
67+
'memory' => 'true'
68+
]);
6569

66-
$entityFactoryClass = \ContainerInteropDoctrine\EntityManagerFactory::class;
70+
$connection->getMapperLocator()->register(PostMapper::class);
71+
```
6772

68-
$persistenceService = new PersistenceService([
69-
PersistenceService::resolveFactoryAlias($entityFactoryClass) => [$entityFactoryClass]
70-
], $app);
73+
Load Mapper by mapper class or entity class
7174

72-
$app->register(new PersistenceServiceProvider($persistenceService));
73-
```
75+
```php
76+
<?php
77+
78+
use Application\Persistence\Mappers\PostMapper;
79+
use Application\Persistence\Entities\Post;
7480

75-
## Examples
81+
// load by mapper
82+
$mapper = $connection->loadMapper(PostMapper::class);
7683

77-
### Full configuration
84+
// load by entity
85+
$mapper = $connection->loadMapper(Post::class);
86+
87+
```
7888

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
8190

82-
### Persistence from Hawbit Application
91+
### Create entity
8392

8493
```php
8594
<?php
8695

87-
/** @var \Hawkbit\Persistence\PersistenceServiceInterface $persistence */
88-
$persistence = $app[\Hawkbit\Persistence\PersistenceServiceInterface::class];
96+
use Application\Persistence\Entities\Post;
8997

90-
$em = $persistence->getEntityManager();
98+
$entity = new Post();
9199

92-
// or with from specific connection
93-
$em = $persistence->getEntityManager('connectionname');
100+
$entity->setContent('cnt');
101+
102+
/** @var Post $createdEntity */
103+
$mapper->create($entity);
94104

95105
```
96106

97-
### Persistence in a Hawkbit controller
98107

99-
Access persistence service in controller. Hawbit is inject classes to controllers by default.
108+
### Load entity
100109

101110
```php
102111
<?php
103112

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+
124115
```
125116

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+
126139
## Change log
127140

128141
Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: marco.bunge
5+
* Date: 05.12.2016
6+
* Time: 10:07
7+
*/
8+
9+
namespace Application\Persistence\Entities;
10+
11+
12+
class Post
13+
{
14+
15+
/**
16+
* @var int
17+
*/
18+
private $id;
19+
20+
/**
21+
* @var string
22+
*/
23+
private $content = '';
24+
25+
/**
26+
* @return int
27+
*/
28+
public function getId()
29+
{
30+
return $this->id;
31+
}
32+
33+
/**
34+
* @param int $id
35+
* @return Post
36+
*/
37+
public function setId($id)
38+
{
39+
$this->id = $id;
40+
return $this;
41+
}
42+
43+
/**
44+
* @return string
45+
*/
46+
public function getContent()
47+
{
48+
return $this->content;
49+
}
50+
51+
/**
52+
* @param string $content
53+
* @return Post
54+
*/
55+
public function setContent($content)
56+
{
57+
$this->content = $content;
58+
return $this;
59+
}
60+
61+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: marco.bunge
5+
* Date: 07.12.2016
6+
* Time: 09:44
7+
*/
8+
9+
namespace Application\Persistence\Mappers;
10+
11+
12+
use Application\Persistence\Entities\Post;
13+
use Doctrine\DBAL\Schema\Column;
14+
use Doctrine\DBAL\Types\Type;
15+
use Hawkbit\Storage\AbstractMapper;
16+
17+
class PostMapper extends AbstractMapper
18+
{
19+
public function define()
20+
{
21+
$this->tableName = 'post';
22+
$this->columns = [
23+
new Column('id', Type::getType(Type::INTEGER)),
24+
new Column('content', Type::getType(Type::TEXT)),
25+
];
26+
$this->primaryKey = ['id'];
27+
$this->entityClass = Post::class;
28+
}
29+
}

example/bootstrap.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
use Application\Persistence\Entities\Post;
4+
use Application\Persistence\Mappers\PostMapper;
5+
use Hawkbit\Storage\ConnectionManager;
6+
7+
require_once __DIR__ . '/../vendor/autoload.php';
8+
9+
/*
10+
* Tis is only an example. All steps are separated in their specific logic
11+
*/
12+
13+
// setup connection
14+
$connection = ConnectionManager::create([
15+
'url' => 'sqlite:///:memory:',
16+
'memory' => 'true'
17+
]);
18+
19+
// setup schema
20+
$connection->exec('CREATE TABLE post (id int, title VARCHAR(255), content TEXT, date DATETIME DEFAULT CURRENT_DATE )');
21+
22+
// register mappers
23+
$connection->getMapperLocator()->register(PostMapper::class);
24+
25+
// load mapper
26+
$entity = new Post();
27+
$mapper = $connection->loadMapper($entity);
28+
29+
// create entity
30+
$entity->setContent('cnt');
31+
$mapper->create($entity);
32+
33+
// find entity by primary key or compound key
34+
$mapper->find(['id' => 1]);
35+
36+
// update entity
37+
$entity->setContent('FOO');
38+
$mapper->update($entity);
39+
40+
// delete entity
41+
$mapper->delete($entity);

0 commit comments

Comments
 (0)