Skip to content

ericmaicon/yii2-repository-pattern

Repository files navigation

Repository Design Pattern for Yii2

Latest Version Build Status

An implementation of repository design pattern.

Martin Fowler described repository as:

A Repository mediates between the domain and data mapping layers, acting like an in-memory domain object collection.

http://martinfowler.com/eaaCatalog/repository.html

This repository tried to fit the design pattern without another one (E.G. Active Record).

Installation

The preferred way to install this extension is through composer.

Either run

$ composer require ericmaicon/yii2-repository-pattern:*

or add

"ericmaicon/yii2-repository-pattern": "*"

to the require section of your composer.json file.

Usage

Append in your config file:

'components' => [
    'repository' => [
        'class' => 'ericmaicon\repository\Gateway',
        'repositories' => [

        ]
    ]
]

You need to identify each repository that you have inside of repositories array. You can identify a Database Repository or a Session Repository:

Database Repository

'test' => [
	'db' => 'db',
],

Database Repositories don't request class and tables attributes. If tables attribute was not filled, the repository will fetch by itself from the database. Otherwise, it is required to fill the db attribute to identify the related database.

Session Repository

'test3' => [
	'class' => 'ericmaicon\repository\session\SessionRepository',
	'tables' => [
    	'user'
    ]
]

The final config section:

'components' => [
    'repository' => [
        'class' => 'ericmaicon\repository\Gateway',
        'repositories' => [
            'test' => [
                'db' => 'db',
            ],
            'test2' => [
                'db' => 'db2',
                'tables' => [
                    'comment'
                ]
            ],
            'test3' => [
                'class' => 'ericmaicon\repository\session\SessionRepository',
                'tables' => [
                    'user'
                ]
            ]
        ]
    ]
]

All repositories models need to extends RepositoryModel:

class User extends RepositoryModel
{

    public $id;
    public $name;

}

You can define the model repository (Optional):

class Sms extends RepositoryModel
{

    public static function repository()
    {
        return 'test2';
    }

}

After that, you can"

Save

$sms = new Sms();
$sms->setAttributes([
	'sms' => '62811112232',
]);
$sms->save();

Update

$sms = Sms::findOne(['id' => $id]);
$sms->setAttributes([
	'sms' => '62811112232',
]);
$sms->update();

Delete

$sms = Sms::findOne(['id' => $id]);
$sms->delete();

Find:

Sms::find(['id' => $id]);
Sms::findOne(['id' => $id]);
Sms::findAll([])

General Doubts

  • If you have the same table name in two different repositories and the repository was not set inside of the model, the model will respond to the first repository in the array.

Testing

$ vagrant up
$ vagrant ssh
$ cd /var/www
$ ./vendor/bin/phpunit

What is missing?

  • RepositoryModel does not have events like ActiveRecord one.
  • DbRepository is not using ActiveRecord. To do so, the SQL part is very rudimentar.
  • Define Class in config array instead of table name

Reference used to build this repository

https://github.com/domnikl/DesignPatternsPHP/tree/master/More/Repository http://culttt.com/2014/09/08/benefits-using-repositories/ http://codereview.stackexchange.com/questions/42498/repository-pattern-with-plain-old-php-object http://code.tutsplus.com/tutorials/the-repository-design-pattern--net-35804 http://moleseyhill.com/blog/2009/07/13/active-record-verses-repository/ http://moleseyhill.com/blog/2009/06/29/simple-repository-pattern/ http://moleseyhill.com/blog/2009/07/06/unit-testing-with-repository-pattern/ http://martinfowler.com/eaaCatalog/repository.html

About

Repository Design Pattern for Yii2

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages