Skip to content

Commit aad1f02

Browse files
authored
Add readme (#5)
* add readme written by ChatGPT * human corrections * review corrections * remove ordered collection limitation
1 parent a3d17fa commit aad1f02

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2024 Bedabox, LLC dba ShipMonk
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# Doctrine Entity Preloader
2+
3+
`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.
4+
5+
- 🚀 **Performance Boost:** Minimizes n+1 issues by preloading related entities with **constant number of queries**.
6+
- 🔄 **Flexible:** Supports all associations: `#[OneToOne]`, `#[OneToMany]`, `#[ManyToOne]`, and `#[ManyToMany]`.
7+
- 💡 **Easy Integration:** Simple to integrate with your existing Doctrine setup.
8+
9+
## Installation
10+
11+
To install the library, use Composer:
12+
13+
```sh
14+
composer require shipmonk/doctrine-entity-preloader
15+
```
16+
17+
## Usage
18+
19+
Below is a basic example demonstrating how to use `EntityPreloader` to preload related entities and avoid the n+1 problem:
20+
21+
```php
22+
use ShipMonk\DoctrineEntityPreloader\EntityPreloader;
23+
24+
$categories = $entityManager->getRepository(Category::class)->findAll();
25+
26+
$preloader = new EntityPreloader($entityManager);
27+
$articles = $preloader->preload($categories, 'articles'); // 1 query to preload articles
28+
$preloader->preload($articles, 'tags'); // 1 query to preload tags
29+
$preloader->preload($articles, 'comments'); // 1 query to preload comments
30+
31+
// no more queries are needed now
32+
foreach ($categories as $category) {
33+
foreach ($category->getArticles() as $article) {
34+
echo $article->getTitle(), "\n";
35+
36+
foreach ($articles->getTags() as $tag) {
37+
echo $tag->getLabel(), "\n";
38+
}
39+
40+
foreach ($articles->getComments() as $comment) {
41+
echo $comment->getText(), "\n";
42+
}
43+
}
44+
}
45+
```
46+
47+
## Comparison vs. Fetch Joins
48+
49+
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.
50+
51+
## Comparison vs. `Doctrine\ORM\AbstractQuery::setFetchMode`
52+
53+
Unlike `setFetchMode` it can
54+
55+
* preload nested associations
56+
* preload `#[ManyToMany]` association
57+
* avoid additional queries fired by Doctrine during hydration process
58+
59+
## Configuration
60+
61+
`EntityPreloader` allows you to adjust batch sizes and fetch join limits to fit your application's performance needs:
62+
63+
- **Batch Size:** Set a custom batch size for preloading to optimize memory usage.
64+
- **Max Fetch Join Same Field Count:** Define the maximum number of join fetches allowed per field.
65+
66+
```php
67+
$preloader->preload(
68+
$articles,
69+
'category',
70+
batchSize: 20,
71+
maxFetchJoinSameFieldCount: 5
72+
);
73+
```
74+
75+
76+
## Limitations
77+
78+
- no support for indexed collections
79+
- no support for dirty collections
80+
- no support for composite primary keys

0 commit comments

Comments
 (0)