shipmonk / doctrine-entity-preloader
Efficient & easy to use solution to n+1 problem in Doctrine ORM
Installs: 5 910
Dependents: 0
Suggesters: 0
Security: 0
Stars: 49
Watchers: 3
Forks: 1
Open Issues: 0
Requires
- php: ^8.1
- doctrine/orm: ^2.19.7 || ^3.2
Requires (Dev)
- doctrine/collections: ^2.2
- doctrine/dbal: ^3.9 || ^4.0
- doctrine/persistence: ^3.3
- editorconfig-checker/editorconfig-checker: ^10.6.0
- ergebnis/composer-normalize: ^2.42.0
- nette/utils: ^4
- phpstan/phpstan: ^1.10.67
- phpstan/phpstan-phpunit: ^1.3.16
- phpstan/phpstan-strict-rules: ^1.5.5
- phpunit/phpunit: ^10.5
- psr/log: ^3
- shipmonk/composer-dependency-analyser: ^1.5
- shipmonk/name-collision-detector: ^2.1.1
- shipmonk/phpstan-rules: ^3.2
- slevomat/coding-standard: ^8.15.0
- symfony/cache: ^6 || ^7
This package is auto-updated.
Last update: 2024-12-12 12:41:27 UTC
README
shipmonk/doctrine-entity-preloader
is a PHP library designed to tackle the n+1 query problem in Doctrine ORM by efficiently preloading related entities. This library offers a flexible and powerful way to optimize database access patterns, especially in cases with complex entity relationships.
- 🚀 Performance Boost: Minimizes n+1 issues by preloading related entities with constant number of queries.
- 🔄 Flexible: Supports all associations:
#[OneToOne]
,#[OneToMany]
,#[ManyToOne]
, and#[ManyToMany]
. - 💡 Easy Integration: Simple to integrate with your existing Doctrine setup (supports both v2 and v3).
Comparison
Comparison vs. Manual Preload
Unlike manual preload, the EntityPreloader does not require writing custom queries for each association.
Comparison vs. Fetch Join
Unlike fetch joins, the EntityPreloader does not fetches duplicate data, which slows down both the query and the hydration process, except when necessary to prevent additional queries fired by Doctrine during hydration process.
The fetch join scales poorly with the number of associations. With every preloaded association, the number of duplicate rows grows. The EntityPreloader does not have this problem.
Comparison vs. setFetchMode
Unlike Doctrine\ORM\AbstractQuery::setFetchMode
it can
- preload nested associations
- preload
#[ManyToMany]
association - avoid additional queries fired by Doctrine during hydration process
Installation
To install the library, use Composer:
composer require shipmonk/doctrine-entity-preloader
Usage
Below is a basic example demonstrating how to use EntityPreloader
to preload related entities and avoid the n+1 problem:
use ShipMonk\DoctrineEntityPreloader\EntityPreloader; $categories = $entityManager->getRepository(Category::class)->findAll(); $preloader = new EntityPreloader($entityManager); $articles = $preloader->preload($categories, 'articles'); // 1 query to preload articles $preloader->preload($articles, 'tags'); // 2 queries to preload tags $preloader->preload($articles, 'comments'); // 1 query to preload comments // no more queries are needed now foreach ($categories as $category) { foreach ($category->getArticles() as $article) { echo $article->getTitle(), "\n"; foreach ($articles->getTags() as $tag) { echo $tag->getLabel(), "\n"; } foreach ($articles->getComments() as $comment) { echo $comment->getText(), "\n"; } } }
Configuration
EntityPreloader
allows you to adjust batch sizes and fetch join limits to fit your application's performance needs:
- Batch Size: Set a custom batch size for preloading to optimize memory usage.
- Max Fetch Join Same Field Count: Define the maximum number of join fetches allowed per field.
$preloader->preload( $articles, 'category', batchSize: 20, maxFetchJoinSameFieldCount: 5 );
Limitations
- no support for indexed collections
- no support for dirty collections
- no support for composite primary keys