PHPackages                             negromovich/doctrine-relations-loader - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. negromovich/doctrine-relations-loader

ActiveLibrary[Database &amp; ORM](/categories/database)

negromovich/doctrine-relations-loader
=====================================

Library for lazy loading relations on Doctrine

1.0.0(6y ago)425.0kMITPHPPHP ^5.5|^7.0

Since Jun 10Pushed 6y agoCompare

[ Source](https://github.com/Negromovich/doctrine-relations-loader)[ Packagist](https://packagist.org/packages/negromovich/doctrine-relations-loader)[ RSS](/packages/negromovich-doctrine-relations-loader/feed)WikiDiscussions master Synced today

READMEChangelogDependencies (1)Versions (2)Used By (0)

Doctrine Relations Loader
=========================

[](#doctrine-relations-loader)

The library is a single class that allows you to load many related entities in an optimal way and performance.

Doctrine UnitOfWork's identityMap used for loading. So method "clear" in UnitOfWork breaks loading.

Usage
-----

[](#usage)

Use blog structure with users, posts, comments and likes for example.

```
/** @ORM\Entity() */
class User
{
    /**
     * @ORM\Id()
     * @ORM\Column(type="integer")
     */
    public $id;

    /** @ORM\Column(type="string") */
    public $name;

    /** @ORM\Column(type="string") */
    public $country;

    /** @ORM\OneToMany(targetEntity="Post", mappedBy="user") */
    public $posts;
}

/** @ORM\Entity() */
class Post
{
    /**
     * @ORM\Id()
     * @ORM\Column(type="integer")
     */
    public $id;

    /** @ORM\Column(type="string") */
    public $title;

    /** @ORM\Column(type="string") */
    public $content;

    /**
     * @ORM\ManyToOne(targetEntity="User")
     * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
     */
    public $user;

    /** @ORM\OneToMany(targetEntity="Comment", mappedBy="post") */
    public $comments;

    /** @ORM\OneToMany(targetEntity="Like", mappedBy="post") */
    public $likes;
}

/** @ORM\Entity() */
class Comment
{
    /**
     * @ORM\Id()
     * @ORM\Column(type="integer")
     */
    public $id;

    /** @ORM\Column(type="string") */
    public $content;

    /**
     * @ORM\ManyToOne(targetEntity="Post")
     * @ORM\JoinColumn(name="post_id", referencedColumnName="id")
     */
    public $post;

    /**
     * @ORM\ManyToOne(targetEntity="User")
     * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
     */
    public $user;
}

/**
 * @ORM\Entity()
 * @ORM\Table(name="`like`")
 */
class Like
{
    /**
     * @ORM\Id()
     * @ORM\Column(type="integer")
     */
    public $id;

    /**
     * @ORM\ManyToOne(targetEntity="Post")
     * @ORM\JoinColumn(name="post_id", referencedColumnName="id")
     */
    public $post;

    /**
     * @ORM\ManyToOne(targetEntity="User")
     * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
     */
    public $user;
}

```

Now we count the number of countries from which we put Like for a specific user (just for example, it is better to use the calculation in the database).

```
$countries = [];
$user = $this->em->getRepository(User::class)->find(1);
foreach ($user->posts as $post) {
    foreach ($post->likes as $like) {
        $countries[$like->user->country] = 1;
    }
}
$num = count($countries);

```

By default, you will perform the number of queries to the database = number of posts + number of users + 2:

```
SELECT * FROM user WHERE id = 1;
SELECT * FROM post WHERE user_id = 1;
SELECT * FROM `like` WHERE post_id = ?; # for each post
SELECT * FROM user WHERE id = ?; # for unique user

```

Queries quantity is too large. I want to merge identical queries to the same table:

```
SELECT * FROM `like` WHERE post_id IN (?); # for all post
SELECT * FROM user WHERE id IN (?); # for all unique users

```

You can use eager loading, but it does not work with a large cascade of entities. Another way to use RelationsLoader from this library.

```
$countries = [];
$user = $this->em->getRepository(User::class)->find(1);

$relationsLoader = new RelationsLoader($this->em);
$relationsLoader->load($user, ['posts' => ['likes' => ['user']]]);

foreach ($user->posts as $post) {
    foreach ($post->likes as $like) {
        $countries[$like->user->country] = 1;
    }
}
$num = count($countries);

```

In this case, 4 queries to the database will be executed.

Syntax
------

[](#syntax)

In `RelationsLoader::load`, you must pass the data for which you want to load the relations (entity, array of entities, collection) and a hierarchical list of relationships to load. How is formed a hierarchical list of consider examples.

- Loading posts for the users (similar to eager loading in doctrine):

    ```
      $relationsLoader->load($users, ['posts']);

    ```
- Loading posts and likes for posts:

    ```
      $relationsLoader->load($users, [
          'posts' => ['likes']
      ]);

    ```
- Loading posts, comments and likes for posts:

    ```
      $relationsLoader->load($users, [
          'posts' => [
              'comments',
              'likes'
          ]
      ]);

    ```
- Loading posts, comments and likes for posts, users for comments:

    ```
      $relationsLoader->load($users, [
          'posts' => [
              'comments' => ['user'],
              'likes'
          ]
      ]);

    ```
- Loading posts, comments and likes for posts, users for comments and likes:

    ```
      $relationsLoader->load($users, [
          'posts' => [
              'comments' => ['user'],
              'likes' => ['user']
          ]
      ]);

    ```

###  Health Score

30

—

LowBetter than 64% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity25

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity54

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

2526d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/1ac725c70d0423910ca1ea9d008b343f636e3c7c09ba6c714c58c7aa13e6ec14?d=identicon)[Negromovich](/maintainers/Negromovich)

---

Top Contributors

[![Negromovich](https://avatars.githubusercontent.com/u/2400061?v=4)](https://github.com/Negromovich "Negromovich (1 commits)")

### Embed Badge

![Health badge](/badges/negromovich-doctrine-relations-loader/health.svg)

```
[![Health](https://phpackages.com/badges/negromovich-doctrine-relations-loader/health.svg)](https://phpackages.com/packages/negromovich-doctrine-relations-loader)
```

###  Alternatives

[doctrine/orm

Object-Relational-Mapper for PHP

10.2k285.3M6.2k](/packages/doctrine-orm)[jdorn/sql-formatter

a PHP SQL highlighting library

3.9k115.1M102](/packages/jdorn-sql-formatter)[illuminate/database

The Illuminate Database package.

2.8k52.4M9.3k](/packages/illuminate-database)[mongodb/mongodb

MongoDB driver library

1.6k64.0M545](/packages/mongodb-mongodb)[ramsey/uuid-doctrine

Use ramsey/uuid as a Doctrine field type.

90340.3M211](/packages/ramsey-uuid-doctrine)[reliese/laravel

Reliese Components for Laravel Framework code generation.

1.7k3.4M16](/packages/reliese-laravel)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
