Downloads
Stars
Version
Unit of Work (Entity Manager) design pattern Laravel implementation.
composer require michalwolinski/unit-of-work
in console to install this library.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 = '[email protected]';
$user->name = 'Michal Wolinski';
$user->password = 'secret';
$user2 = new User();
$user2->email = '[email protected]';
$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();
}
}
This project is licensed under the MIT License.