https://github.com/dorofey/depository
ZF-3 Data mapper module
https://github.com/dorofey/depository
datamapper mapper
Last synced: 28 days ago
JSON representation
ZF-3 Data mapper module
- Host: GitHub
- URL: https://github.com/dorofey/depository
- Owner: dorofey
- Created: 2017-10-20T09:28:36.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-11-26T19:36:49.000Z (about 8 years ago)
- Last Synced: 2026-01-14T14:46:44.899Z (30 days ago)
- Topics: datamapper, mapper
- Language: PHP
- Homepage:
- Size: 36.1 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# ZF-3 Data mapper module
Simple Data Mapper implementation based on `zend-db`
This is not just work in progress, but beginning of work in progress. Don't even think about using it.
## Example:
### Define our Post model
EntityInterface defines getter and setter for id, nothing more.
```php
class Post implements EntityInterface
{
public $id;
public $user;
public $title;
public function setId($id)
...
public function getId()
...
}
```
### Define our mapper
```php
class PostMapper extends Repository\Mapper\StandardMapper
{
protected static $entityClass = Post::class;
protected static $table = 'posts';
protected static $features = [
Repository\Mapper\Feature\Relations::class => [
'user' => [Repository\Hydrator\HasOne::class, User::class]
]
];
}
```
### Add stuff in configuration
```php
[
'mappers' => [
'maps' => [
Post::class => PostMapper::class
],
'aliases' => [
'posts' => Post::class
]
]
]
```
### In your code
```php
$repository = $serviceLocator->get(\Repository\Repository\RepositoryPluginManager::class);
$mapper = $repository->get(Post::class);
$singlePost = $mapper->id(10);
$allPosts = $mapper->fetch();
$somePosts = $mapper->fetch(['title' => 'My post title']);
$somePosts = $mapper->fetch(function(Select $select){
$select->where(['title' => 'My post title'])->order('cteated_at')->limit(2);
});
```
## Features
### \Repository\Mapper\Feature\SoftDelete
Enables "soft-delete" feature. Exposes `recover` method
```php
// In your mapper
protected static $features = [
Repository\Mapper\Feature\SoftDelete::class => 'deleted_at' // default field is 'deleted_at'
];
....
$post = $mapper->id(10);
$mapper->delete($post);
$mapper->recover($post);
```
### \Repository\Mapper\Feature\Timestamps
Enables created and updated fields
```php
// In your mapper
protected static $features = [
Repository\Mapper\Feature\Timestamps::class => ['created_at', 'updated_at']
];
....
```
### \Repository\Mapper\Feature\Transaction
```php
// In your mapper
protected static $features = [
Repository\Mapper\Feature\Transaction::class,
];
....
$mapper->withTransaction();
$mapper->update($post1);
$mapper->update($post2);
$mapper->insert($post3);
$mapper->delete($post4);
$mapper->commitTransaction();
```
### \Repository\Mapper\Feature\Relations
```php
// In your mapper
protected static $features = [
Repository\Mapper\Feature\Relations::class => [
'users' => [HasManyThrough::class, User::class, ['post', 'user'], 'post_users'],
'author' => [HasOne::class, User::class],
],
];
....
$post = $mapper->withRelation(['users', 'author])->id(10);
$post->author->name;
```
### \Repository\Mapper\Feature\SelectStrategy (Work in progress)
Lets you define custom query logic or hide implementation details.
Would be useful if you're going to expose mappers to a ViewHelper or anything monkey would have access too.
```php
// In your mapper
protected static $features = [
Repository\Mapper\Feature\SelectStrategy::class,
];
....
$post = $mapper->withStrategy(['limit' => 10, 'where' => 'author=2,age<55', 'order' => '-created_at'])->fetch();
// Or simply
$post = $mapper->fetchWithStrategy(['limit' => 10, 'where' => 'author=2,age<55']);
```