承接 negromovich/doctrine-relations-loader 相关项目开发

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

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

negromovich/doctrine-relations-loader

Composer 安装命令:

composer require negromovich/doctrine-relations-loader

包简介

Library for lazy loading relations on Doctrine

README 文档

README

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

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

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']
          ]
      ]);
    

negromovich/doctrine-relations-loader 适用场景与选型建议

negromovich/doctrine-relations-loader 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 25.04k 次下载、GitHub Stars 达 4, 最近一次更新时间为 2019 年 06 月 10 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 negromovich/doctrine-relations-loader 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 negromovich/doctrine-relations-loader 我们能提供哪些服务?
定制开发 / 二次开发

基于 negromovich/doctrine-relations-loader 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

  • Stars: 4
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-06-10