PHPackages                             loophp/repository-monadic-helper - 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. [Database &amp; ORM](/categories/database)
4. /
5. loophp/repository-monadic-helper

ActiveLibrary[Database &amp; ORM](/categories/database)

loophp/repository-monadic-helper
================================

Monadic Doctrine repositories helper classes and services.

1.1.0(3y ago)171.9k↓92.9%[1 issues](https://github.com/loophp/repository-monadic-helper/issues)MITPHPPHP &gt;= 8.1CI passing

Since Dec 9Pushed 6d ago1 watchersCompare

[ Source](https://github.com/loophp/repository-monadic-helper)[ Packagist](https://packagist.org/packages/loophp/repository-monadic-helper)[ GitHub Sponsors](https://github.com/drupol)[ RSS](/packages/loophp-repository-monadic-helper/feed)WikiDiscussions main Synced 3d ago

READMEChangelog (10)Dependencies (5)Versions (18)Used By (0)

[![Latest Stable Version](https://camo.githubusercontent.com/553b04fa1b698766bcf60dbb38bc6ae645274db42f35c404f64370e7288fdbbe/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6c6f6f7068702f7265706f7369746f72792d6d6f6e616469632d68656c7065722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/loophp/repository-monadic-helper)[![GitHub stars](https://camo.githubusercontent.com/9615468a17c91aef9d1ec03b36e71f905e97ff84f167e82993011332975fd2ee/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f73746172732f6c6f6f7068702f7265706f7369746f72792d6d6f6e616469632d68656c7065722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/loophp/repository-monadic-helper)[![Total Downloads](https://camo.githubusercontent.com/7c5178d8b82c001eab7218fe36be71cd9d7718ea30cfeccafcba9a5f50a3d92b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6c6f6f7068702f7265706f7369746f72792d6d6f6e616469632d68656c7065722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/loophp/repository-monadic-helper)[![GitHub Workflow Status](https://camo.githubusercontent.com/afbd3b4074b69cf4d7c035fe5d17f4a0fc9f96dc133d1d77c3208e1dff21b325/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6c6f6f7068702f7265706f7369746f72792d6d6f6e616469632d68656c7065722f74657374732e796d6c3f6272616e63683d6d61696e267374796c653d666c61742d737175617265)](https://github.com/loophp/repository-monadic-helper/actions)[![Type Coverage](https://camo.githubusercontent.com/f2d0786f2318f60acc14de59d870c05509be99630fd41960d25c5a2fefa94dc4/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f64796e616d69632f6a736f6e3f7374796c653d666c61742d73717561726526636f6c6f723d636f6c6f72266c6162656c3d54797065253230636f7665726167652671756572793d6d6573736167652675726c3d687474707325334125324625324673686570686572642e6465762532466769746875622532466c6f6f7068702532467265706f7369746f72792d6d6f6e616469632d68656c706572253246636f766572616765)](https://shepherd.dev/github/loophp/repository-monadic-helper)[![License](https://camo.githubusercontent.com/e14bef7b336c4dcf9185d06a8930e930ad34ad798b046f84ab4fdb0a1d87378a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6c6f6f7068702f7265706f7369746f72792d6d6f6e616469632d68656c7065722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/loophp/repository-monadic-helper)[![Donate!](https://camo.githubusercontent.com/a71f45de7e408be2477113d166e9ee94c90bbf814a1373fd154aa5b6652302de/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f53706f6e736f722d4769746875622d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](https://github.com/sponsors/drupol)

Doctrine Repository Monadic Helper
==================================

[](#doctrine-repository-monadic-helper)

Description
-----------

[](#description)

This project provides the necessary classes and services to use Doctrine repositories in a more functional way, by using monads.

This project also demonstrate that it's a nice and clean way to work with repositories and non-deterministic data store, in this case, a database.

There is no need to always check for the existence of an entity, so we are able to reduce the amount of conditions and cruft, while focusing on what's important and relevant only.

When using properly typed monads and callbacks, types inconsistencies will be instantly detected by static analysis tools. This provides a safer and better way to design functions and data transformation methods.

The monad in use in this project is the *Either* monad, provided by the contrib package [Lamphpda](https://github.com/marcosh/lamphpda) from [Marco Perone](https://github.com/marcosh/).

History
-------

[](#history)

This project started as a proof-of-concept for my own needs and for many reasons.

I first wanted to get rid of all the checks and conditions that were needed in my code to test if an entity was existing or not.

Then, it turns out that this was a recurrent pattern that I was seeing the code of my friends, colleagues and [issue queue](https://github.com/doctrine/persistence/issues/23).

A practical way to get rid of conditions is to use a more declarative way of programming and adopt a more functional programming style.

And lastly, willing to learn more about the monads which are some kind of "design patterns" for functional programming, I started to write this package.

This package does not have the pretention to become a *de-facto* standard on how to use Doctrine repositories, but it might help people understanding what monads are, how to use them, and hopefully reduce the amount of conditions in their code.

The monad package in use here is an arbitrary choice. I could have used some other packages, but [marcosh/lamphpda](https://github.com/marcosh/lamphpda) seems to be the best option, especially when you analyse your code with static analysis tools to detect issues upfront, without running their unit tests.

Installation
------------

[](#installation)

```
composer require loophp/repository-monadic-helper
```

Usage
-----

[](#usage)

To use this package and have monadic repositories, there are 3 options available:

- Without alteration of existing Doctrine repositories
    - **Options 1**: By using a service which is creating a monadic repository from an entity class name or an existing repository.
- With alteration of existing Doctrine repositories
    - **Option 2**: By adding an interface, a trait and relevant typing information like: `@implements MonadicServiceEntityRepositoryInterface`
    - **Option 3**: By replacing `extends ServiceEntityRepository` with `extends MonadicServiceEntityRepository` and add relevant typing information like: `@extends MonadicServiceEntityRepository`

In my own opinion, the best way to use this package is to **use the first option**.

It's paramount to replace `EntityClassName` with the entity class in use in the repository in order to let static analysis tools infer types properly.

For the option 2 and 3, let's use the following usual Doctrine repository as example:

```
