Download the PHP package shlinkio/doctrine-specification without Composer
On this page you can find all versions of the php package shlinkio/doctrine-specification. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download shlinkio/doctrine-specification
More information about shlinkio/doctrine-specification
Files in shlinkio/doctrine-specification
Package doctrine-specification
Short Description Specification Pattern for your Doctrine repositories. Forked from https://github.com/Happyr/Doctrine-Specification
License MIT
Homepage http://developer.happyr.com/
Informations about the package doctrine-specification
Happyr Doctrine Specification
This library gives you a new way for writing queries. Using the Specification pattern you will get small Specification classes that are highly reusable.
The problem with writing Doctrine queries is that it soon will be messy. When your application grows you will have 20+ function in your Doctrine repositories. All with long and complicated QueryBuilder calls. You will also find that you are using a lot of parameters to the same method to accommodate different use cases.
After a discussion with Kacper Gunia on Sound of Symfony podcast about how to test your Doctrine repositories properly, we (Kacper and Tobias) decided to create this library. We have been inspired by Benjamin Eberlei's thoughts in his blog post.
Table of contents
- basic understanding (this page)
- Usage examples
- Create your own spec
- Contributing to the library
Why do we need this lib?
You are probably wondering why we created this library. Your entity repositories are working just fine as they are, right?
But if your friend open one of your repository classes he/she would probably find that the code is not as perfect as you thought. Entity repositories have a tendency to get messy. Problems may include:
- Too many functions (
findActiveUser
,findActiveUserWithPicture
,findUserToEmail
, etc) - Some functions have too many arguments
- Code duplication
- Difficult to test
Requirements of the solution
The solution should have the following features:
- Easy to test
- Easy to extend, store and run
- Re-usable code
- Single responsibility principle
- Hides the implementation details of the ORM. (This might seen like nitpicking, however it leads to bloated client code doing the query builder work over and over again.)
The practical differences
This is an example of how you use the lib. Say that you want to fetch some Adverts and close them. We should select all Adverts that have their endDate
in the past. If endDate
is null make it 4 weeks after the startDate
.
Yes, it looks pretty much the same. But the later is reusable. Say you want another query to fetch Adverts that we should close but only for a specific company.
Doctrine Specification
QueryBuilder
If you were about to do the same thing with only the QueryBuilder it would look like this:
The issues with the QueryBuilder implementation are:
- You may only use the filters
filterOwnedByCompany
andfilterAdvertsWeShouldClose
inside AdvertRepository. - You can not build a tree with And/Or/Not. Say that you want every Advert but not those owned by $company. There
is no way to reuse
filterOwnedByCompany()
in that case. - Different parts of the QueryBuilder filtering cannot be composed together, because of the way the API is created. Assume we have a filterGroupsForApi() call, there is no way to combine it with another call filterGroupsForPermissions(). Instead reusing this code will lead to a third method filterGroupsForApiAndPermissions().
Check single entity
You can apply specifications to validate specific entities or dataset.
Filter collection
You can apply specifications to filter collection of entities or datasets.
Continue reading
You may want to take a look at some usage examples or find out how to create your own spec.
All versions of doctrine-specification with dependencies
doctrine/orm Version ^3.0 || ^2.18
symfony/property-access Version ^7.0 || ^6.0
symfony/polyfill-php80 Version ^1.20