PHPackages                             dorofey/zf3-data-mapper - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. dorofey/zf3-data-mapper

ActiveLibrary

dorofey/zf3-data-mapper
=======================

Simple Zf3 data mapper

0.0.1(8y ago)16MITPHP

Since Oct 20Pushed 8y ago1 watchersCompare

[ Source](https://github.com/dorofey/depository)[ Packagist](https://packagist.org/packages/dorofey/zf3-data-mapper)[ RSS](/packages/dorofey-zf3-data-mapper/feed)WikiDiscussions master Synced today

READMEChangelog (2)Dependencies (4)Versions (2)Used By (0)

ZF-3 Data mapper module
=======================

[](#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:
--------

[](#example)

### Define our Post model

[](#define-our-post-model)

EntityInterface defines getter and setter for id, nothing more.

```
class Post implements EntityInterface
{
    public $id;
    public $user;
    public $title;

    public function setId($id)
    ...
    public function getId()
    ...
}
```

### Define our mapper

[](#define-our-mapper)

```
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

[](#add-stuff-in-configuration)

```
[
    'mappers' => [
        'maps' => [
            Post::class => PostMapper::class
        ],
        'aliases' => [
            'posts' => Post::class
        ]
    ]
]
```

### In your code

[](#in-your-code)

```
$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
--------

[](#features)

### \\Repository\\Mapper\\Feature\\SoftDelete

[](#repositorymapperfeaturesoftdelete)

Enables "soft-delete" feature. Exposes `recover` method

```
// 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

[](#repositorymapperfeaturetimestamps)

Enables created and updated fields

```
// In your mapper
protected static $features = [
    Repository\Mapper\Feature\Timestamps::class => ['created_at', 'updated_at']
];
....
```

### \\Repository\\Mapper\\Feature\\Transaction

[](#repositorymapperfeaturetransaction)

```
// 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

[](#repositorymapperfeaturerelations)

```
// 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)

[](#repositorymapperfeatureselectstrategy-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.

```
// In your mapper
protected static $features = [
    Repository\Mapper\Feature\SelectStrategy::class,
];
....

$post = $mapper->withStrategy(['limit' => 10, 'where' => 'author=2,age
