Download the PHP package verclam/smart-fetch-bundle without Composer
On this page you can find all versions of the php package verclam/smart-fetch-bundle. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download verclam/smart-fetch-bundle
More information about verclam/smart-fetch-bundle
Files in verclam/smart-fetch-bundle
Package smart-fetch-bundle
Short Description This bundle will help avoiding make n+1 in symfony doctrine entities
License MIT
Informations about the package smart-fetch-bundle
Intro to Verclam/Smart-Fetch-Bundle
Enhance your project performance by avoiding N+1 queries with Smart Fetch Bundle.
Latest updates
For notes about the latest changes please read CHANGELOG
,
for required changes in your code please read UPGRADE
chapter of the documentation.
Requirements:
- Verclam/Smart-Fetch-Bundle's master is compatible with Symfony
>=5.4
versions. - "doctrine/persistence" and "doctrine/doctrine-bundle"
>=2.2
version is required. - PHP
>=8.0
version is required.
Installation :
Pretty simple with Composer, run
Usage :
1 - Configuration
Nothing to do, the bundle is ready to use, you can start using it and implement it directly to you project.
2 - Use in controller's method
To use the bundle in a controller's method, you have to add the #[SmartFetch]
attribute to the parameter or the method.
queryName
: The name of the query parameter in the URL that will be used to fetch the entity 'mandatory'.class
: The class of the entity that will be fetched 'mandatory'.joinEntities
: The relations that will be fetched, the order of the relation is important.argumentName
: The name of the variable in the method's parameter 'mandatory if you fetch more than one entity'.
3 - Use in controller's parameter
queryName
: The name of the query parameter in the URL that will be used to fetch the entity 'mandatory'.joinEntities
: The relations that will be fetched, the order of the relation is important.argumentName
: Not mandatory.class
: Not mandatory.
Entities
Suppose that you have a project when a User
can post Article
and Comment
, a User
can also like a Comment
, and an Article
can belong to many Category
.
The entities for this project will be like this:
User
Article
Comment
Category
Controller
Now, suppose that you want to get a User
with all his Article
and Comment
and all the Category
of each Article
and all the User
who liked each Comment
of each Article
, and to show it in a view, the controller and view.
Controller
View
What do you think Symfony and Doctrine will do ?
Explosion, yes because the User
in the controller will be fetched with this simple SQL query:
All the relation will be a Proxy
and every time you will access to a relation, a new SQL query will be executed to fetch only one entity.
It's easy when the view in rendered, if you take a look on the Symfony profiler toolbar, in the Doctrine
section , you will see it in Yellow
color in other terms, Warning
it's probably a N+1
query.
Here is the query metrics:
How to avoid this ?
With Smart Fetch Bundle
you can fetch all the relations that you want, and you can also fetch the relations of the relations, and the relations of the relations of the relations, and so on in only ONE QUERY
.
How to do it ?
After installing the bundle, the only thing that you have to do is to add the #[SmartFetch]
attribute to the property or the methode and indicate what do you want to fetch.
Example:
Controller:
On the method:
On the parameter:
What do you think Symfony and Doctrine will do ?
Nothing bad, the User
and all the relations that you want will be fetched in only one query.
The query metrics will be like this:
Isn't it amazing ? 😍
Don't waste your time, install the bundle and enjoy it.
PS: If you're thinking of using this bundle to retrieve several hundred entities in a single query, take a pause and think again, because it won't be reasonable, Doctrine will lose his mind when hydrating thousands of rows to the entity, so our advice is to use this bundle to find the right balance between accepting a certain degree of n+1 and making joins.
All versions of smart-fetch-bundle with dependencies
symfony/config Version ^5.0|^6.0
symfony/dependency-injection Version ^5.0|^6.0
symfony/framework-bundle Version ^5.0|^6.0
symfony/http-kernel Version ^5.0|^6.0
doctrine/persistence Version ^2.2 || ^3.0
doctrine/doctrine-bundle Version ^2.2 || ^3.0