michalwolinski / unit-of-work
Unit of Work (Entity Manager) design pattern Laravel implementation.
dev-master
2020-02-05 17:39 UTC
Requires
- php: ^7.4
- laravel/framework: ^6.0
Requires (Dev)
- mockery/mockery: ^1.0
- phpunit/phpunit: ^9.0@dev
This package is auto-updated.
Last update: 2024-05-06 03:10:46 UTC
README
Unit of Work (Entity Manager) design pattern Laravel implementation.
Installation by Composer
- Run
composer require michalwolinski/unit-of-work
in console to install this library.
Usage
I propose to use Dependency Injection to inject UnitOfWorkInterface
.
Example implementation in service class:
use App\User; use MichalWolinski\UnitOfWork\Interfaces\UnitOfWorkInterface; class Service { /** * @var UnitOfWorkInterface */ private UnitOfWorkInterface $unitOfWork; public function __construct(UnitOfWorkInterface $unitOfWork) { $this->unitOfWork = $unitOfWork; } public function example() { $user = new User(); $user->email = 'firma@haxmedia.pl'; $user->name = 'Michal Wolinski'; $user->password = 'secret'; $user2 = new User(); $user2->email = 'example@company.com'; $user2->name = 'John Doe'; $user2->password = 'secret'; // CREATE RECORDS $this->unitOfWork->insert($user); $this->unitOfWork->insert($user2); $this->unitOfWork->commit(); dump($user2->getKey()); // UPDATE RECORDS $user2->name = 'Jane Doe'; $this->unitOfWork->update($user2); $this->unitOfWork->commit(); // REMOVE RECORDS $this->unitOfWork->delete($user2); $this->unitOfWork->commit(); } }
Authors
- Michal Wolinski - Haxmedia
License
This project is licensed under the MIT License.