Skip to content

Commit fe50033

Browse files
committed
init
0 parents  commit fe50033

22 files changed

+1724
-0
lines changed

.editorconfig

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[*]
2+
charset = utf-8
3+
end_of_line = lf
4+
trim_trailing_whitespace = true
5+
insert_final_newline = true
6+
indent_style = space
7+
indent_size = 4

.gitattributes

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
* text=auto
2+
3+
/.github export-ignore
4+
/cache export-ignore
5+
/tests export-ignore
6+
7+
/.editorconfig export-ignore
8+
/.gitattributes export-ignore
9+
/.gitignore export-ignore
10+
/phpcs.xml.dist export-ignore
11+
/phpstan.neon.dist export-ignore
12+
/phpunit.xml.dist export-ignore

.github/workflows/checks.yml

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
name: Checks
2+
on:
3+
pull_request:
4+
push:
5+
branches:
6+
- "master"
7+
- "v[0-9]"
8+
jobs:
9+
checks:
10+
runs-on: ubuntu-latest
11+
strategy:
12+
fail-fast: false
13+
steps:
14+
-
15+
name: Checkout code
16+
uses: actions/checkout@v4
17+
-
18+
name: Setup PHP
19+
uses: shivammathur/setup-php@v2
20+
with:
21+
php-version: 8.3
22+
-
23+
name: Install dependencies
24+
run: composer install --no-progress --prefer-dist --no-interaction
25+
26+
-
27+
name: Run checks
28+
run: composer check
29+
30+
tests:
31+
runs-on: ubuntu-latest
32+
strategy:
33+
fail-fast: false
34+
matrix:
35+
php-version: [ '8.1', '8.2', '8.3' ]
36+
dependency-version: [ prefer-lowest, prefer-stable ]
37+
steps:
38+
-
39+
name: Checkout code
40+
uses: actions/checkout@v4
41+
-
42+
name: Setup PHP
43+
uses: shivammathur/setup-php@v2
44+
with:
45+
php-version: ${{ matrix.php-version }}
46+
-
47+
name: Install dependencies
48+
run: composer update --no-progress --${{ matrix.dependency-version }} --prefer-dist --no-interaction
49+
-
50+
name: Run tests
51+
run: composer check:tests

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/cache
2+
/vendor
3+
/phpstan.neon
4+
/composer.lock

LICENSE

+21
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

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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+
10+
## Comparison
11+
12+
| | Default | Manual Preload | Fetch Join | setFetchMode | **EntityPreloader** |
13+
|------------------------------------------------------------------------|------------|--------------------------|------------------------|--------------|--------------------------------|
14+
| [OneToMany](tests/EntityPreloadBlogOneHasManyTest.php) | 1 + n | impossible in Doctrine 3 | 1, but duplicates rows | 1 + 1 | 1 + 1 |
15+
| [OneToManyDeep](tests/EntityPreloadBlogOneHasManyDeepTest.php) | 1 + n + n² | impossible in Doctrine 3 | 1, but duplicates rows | 1 + 1 + n² | 1 + 1 + 1 |
16+
| [OneToManyAbstract](tests/EntityPreloadBlogOneHasManyAbstractTest.php) | 1 + n + n² | impossible in Doctrine 3 | 1, but duplicates rows | 1 + 1 + n² | 1 + 1 + 1, but duplicates rows |
17+
| [ManyToOne](tests/EntityPreloadBlogManyHasOneTest.php) | 1 + n | 1 + 1 | 1, but duplicates rows | 1 + 1 | 1 + 1 |
18+
| [ManyToOneDeep](tests/EntityPreloadBlogManyHasOneDeepTest.php) | 1 + n + n | 1 + 1 + 1 | 1, but duplicates rows | 1 + 1 + n | 1 + 1 + 1 |
19+
| [ManyToMany](tests/EntityPreloadBlogManyHasManyTest.php) | 1 + n | impossible in Doctrine 3 | 1, but duplicates rows | 1 + n | 1 + 1 |
20+
21+
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.
22+
23+
Unlike `Doctrine\ORM\AbstractQuery::setFetchMode` it can
24+
25+
* preload nested associations
26+
* preload `#[ManyToMany]` association
27+
* avoid additional queries fired by Doctrine during hydration process
28+
29+
30+
## Installation
31+
32+
To install the library, use Composer:
33+
34+
```sh
35+
composer require shipmonk/doctrine-entity-preloader
36+
```
37+
38+
## Usage
39+
40+
Below is a basic example demonstrating how to use `EntityPreloader` to preload related entities and avoid the n+1 problem:
41+
42+
```php
43+
use ShipMonk\DoctrineEntityPreloader\EntityPreloader;
44+
45+
$categories = $entityManager->getRepository(Category::class)->findAll();
46+
47+
$preloader = new EntityPreloader($entityManager);
48+
$articles = $preloader->preload($categories, 'articles'); // 1 query to preload articles
49+
$preloader->preload($articles, 'tags'); // 1 query to preload tags
50+
$preloader->preload($articles, 'comments'); // 1 query to preload comments
51+
52+
// no more queries are needed now
53+
foreach ($categories as $category) {
54+
foreach ($category->getArticles() as $article) {
55+
echo $article->getTitle(), "\n";
56+
57+
foreach ($articles->getTags() as $tag) {
58+
echo $tag->getLabel(), "\n";
59+
}
60+
61+
foreach ($articles->getComments() as $comment) {
62+
echo $comment->getText(), "\n";
63+
}
64+
}
65+
}
66+
```
67+
68+
## Configuration
69+
70+
`EntityPreloader` allows you to adjust batch sizes and fetch join limits to fit your application's performance needs:
71+
72+
- **Batch Size:** Set a custom batch size for preloading to optimize memory usage.
73+
- **Max Fetch Join Same Field Count:** Define the maximum number of join fetches allowed per field.
74+
75+
```php
76+
$preloader->preload(
77+
$articles,
78+
'category',
79+
batchSize: 20,
80+
maxFetchJoinSameFieldCount: 5
81+
);
82+
```
83+
84+
85+
## Limitations
86+
87+
- no support for indexed collections
88+
- no support for dirty collections
89+
- no support for composite primary keys

composer.json

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
{
2+
"name": "shipmonk/doctrine-query-checker",
3+
"description": "Doctrine Query AST validator",
4+
"license": [
5+
"MIT"
6+
],
7+
"require": {
8+
"php": "^8.1",
9+
"doctrine/dbal": "^4.2",
10+
"doctrine/orm": "^3.2"
11+
},
12+
"require-dev": {
13+
"editorconfig-checker/editorconfig-checker": "^10.6.0",
14+
"ergebnis/composer-normalize": "^2.42.0",
15+
"nette/utils": "^4.0",
16+
"phpstan/phpstan": "^2.0",
17+
"phpstan/phpstan-phpunit": "^2.0",
18+
"phpstan/phpstan-strict-rules": "^2.0",
19+
"phpunit/phpunit": "^12",
20+
"ramsey/uuid": "^4.7.6",
21+
"ramsey/uuid-doctrine": "^2.1.0",
22+
"shipmonk/composer-dependency-analyser": "^1.8",
23+
"shipmonk/name-collision-detector": "^2.1",
24+
"shipmonk/phpstan-rules": "^4.1",
25+
"slevomat/coding-standard": "^8.15",
26+
"symfony/cache": "^7.0"
27+
},
28+
"autoload": {
29+
"psr-4": {
30+
"ShipMonk\\DoctrineQueryChecker\\": "src/"
31+
}
32+
},
33+
"autoload-dev": {
34+
"psr-4": {
35+
"ShipMonkTests\\DoctrineQueryChecker\\": "tests/"
36+
}
37+
},
38+
"config": {
39+
"allow-plugins": {
40+
"dealerdirect/phpcodesniffer-composer-installer": true,
41+
"ergebnis/composer-normalize": true
42+
},
43+
"sort-packages": true
44+
},
45+
"scripts": {
46+
"check": [
47+
"@check:composer",
48+
"@check:ec",
49+
"@check:cs",
50+
"@check:types",
51+
"@check:tests",
52+
"@check:dependencies"
53+
],
54+
"check:composer": [
55+
"composer normalize --dry-run --no-check-lock --no-update-lock",
56+
"composer validate --strict"
57+
],
58+
"check:cs": "phpcs",
59+
"check:dependencies": "composer-dependency-analyser",
60+
"check:ec": "ec src tests",
61+
"check:tests": "phpunit tests",
62+
"check:types": "phpstan analyse -vvv",
63+
"fix:cs": "phpcbf"
64+
}
65+
}

0 commit comments

Comments
 (0)