承接 shipmonk/doctrine-entity-preloader 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

shipmonk/doctrine-entity-preloader

最新稳定版本:1.4.0

Composer 安装命令:

composer require shipmonk/doctrine-entity-preloader

包简介

Efficient & easy to use solution to n+1 problem in Doctrine ORM

README 文档

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

Default Manual Fetch Join setFetchMode EntityPreloader
OneToMany 🔴 1 + n 🟢 2 🟠 1, but
duplicate rows
🟢 2 🟢 2
OneToManyDeep 🔴 1 + n + n² 🟢 3 🟠 1, but
duplicate rows
🔴 2 + n² 🟢 3
OneToManyAbstract 🔴 1 + n + n² 🟠 3, but
duplicate rows
🟠 1, but
duplicate rows
🔴 2 + n² 🟠 3, but
duplicate rows
ManyToOne 🔴 1 + n 🟢 2 🟠 1, but
duplicate rows
🟢 2 🟢 2
ManyToOneDeep 🔴 1 + n + n 🟢 3 🟠 1, but
duplicate rows
🔴 2 + n 🟢 3
ManyToMany 🔴 1 + n 🟢 2 🟠 1, but
duplicate rows
🔴 1 + n 🟢 2

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

PHPStan

This library provides PHPStan integration:

  • extension.neon - Infers return types for EntityPreloader::preload() based on the preloaded association
  • rules.neon - Validates that the property name passed to preload() exists on the entity

If you use PHPStan and have phpstan/extension-installer installed, the extension and rules are enabled automatically.

Otherwise, add the following to your phpstan.neon:

includes:
    - vendor/shipmonk/doctrine-entity-preloader/extension.neon
    - vendor/shipmonk/doctrine-entity-preloader/rules.neon

If phpstan/phpstan-doctrine is installed and objectManagerLoader is used, all mapping formats become available (xml, phpdoc, yaml). Otherwise, only modern attributes are supported.

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 ($article->getTags() as $tag) {
            echo $tag->getLabel(), "\n";
        }

        foreach ($article->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

统计信息

  • 总下载量: 97.22k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 94
  • 点击次数: 1
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 93
  • Watchers: 3
  • Forks: 4
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-10-07