lenny4/doctrine-merge-persistent-collection-bundle
Composer 安装命令:
composer require lenny4/doctrine-merge-persistent-collection-bundle
包简介
Merge persistence collection when update entity
README 文档
README
Merge persistence collection when update entity
This bundle is very usefull if you are using ApiPlatform and you don't want to send multiple requests when you update an entity and his children.
Installation
You can install the package via composer:
composer require lenny4/doctrine-merge-persistent-collection-bundle
Usage
<?php namespace App\Controller; use Lenny4\DoctrineMergePersistentCollectionBundle\DoctrineMergePersistentCollection; use Lenny4\DoctrineMergePersistentCollectionBundle\Entity\Father; use Lenny4\DoctrineMergePersistentCollectionBundle\Entity\Son; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; class PutFatherController extends AbstractController { public function __invoke(Father $data, DoctrineMergePersistentCollection $doctrineMergePersistentCollection): Father { $doctrineMergePersistentCollection->merge( $data->getSons(), static function (Son $son1, Son $son2) { return $son1->getName() === $son2->getName(); }, static function (Son $son1, Son $son2) { $son1->setAge($son2->getAge()); }, ); return $data; } }
How does it work ?
Let's say you have 2 entities Father and Son as ApiResource
#[ApiResource(
collectionOperations: [
'get',
],
itemOperations: [
'get',
'put' => [
'controller' => PutFatherController::class,
],
],
attributes: [
'denormalization_context' => ['groups' => ['w-father']],
],
)]
#[ORM\Entity(repositoryClass: FatherRepository::class)]
class Father
{
#[ORM\Column]
#[ORM\GeneratedValue]
#[ORM\Id]
private ?int $id = null;
#[ORM\Column(length: 255)]
#[Groups(['w-father'])]
private ?string $name = null;
/**
* @var Collection<int, Son>|Son[]
*/
#[ORM\OneToMany(mappedBy: 'father', targetEntity: Son::class, cascade: ['persist', 'remove'], orphanRemoval: true)]
#[Groups(['w-father'])]
private Collection $sons;
// getters setters
}
#[ApiResource()] #[ORM\Entity(repositoryClass: SonRepository::class)] class Son { #[ORM\Column] #[ORM\GeneratedValue] #[ORM\Id] private ?int $id = null; #[ORM\Column(length: 255)] #[Groups(['w-father'])] private ?string $name = null; #[ORM\Column] #[Groups(['w-father'])] private ?int $age = null; #[ORM\JoinColumn(nullable: false)] #[ORM\ManyToOne(inversedBy: 'sons')] private ?Father $father = null; // getters setters }
With these data in your database:
| id | name |
|---|---|
| 1 | father1 |
| id | name | age | father_id |
|---|---|---|---|
| 1 | son1 | 10 | 1 |
| 2 | son2 | 20 | 1 |
Let's say you want to update the name of the Father from father1 to father2 and the age of 1 of his
children son1 only with 1 request. It's currently impossible only
with ApiPlatform. But with this Bundle you can do it now by
calling doctrineMergePersistentCollection->merge as shown in the Usage.
- The first argument of the function take a
PersistentCollectionwhich correspond to all the sons of the father - The second argument is a callable which define how 2 sons are the same (in our example it's the
name
$son1->getName() === $son2->getName()) - The second argument is a callable which define how to update a son if
$son1->getName() === $son2->getName()return true;
Use case (example)
- Add a son
PUT /api/fathers/1
{
"name": "father2",
"sons": [
{
"name": "son1",
"age": 10
},
{
"name": "son2",
"age": 20
},
{
"name": "son3",
"age": 30
}
]
}
This will change the name of the father to father2 and add a son. Result:
| id | name | age | father_id |
|---|---|---|---|
| 1 | son1 | 10 | 1 |
| 2 | son2 | 20 | 1 |
| 3 | son3 | 30 | 1 |
- Update son2 age
PUT /api/fathers/1
{
"name": "father2",
"sons": [
{
"name": "son1",
"age": 10
},
{
"name": "son2",
"age": 30
}
]
}
This will change the name of the father to father2 and son2 age. Result:
| id | name | age | father_id |
|---|---|---|---|
| 1 | son1 | 10 | 1 |
| 2 | son2 | 30 | 1 |
Testing
docker-compose up
./runc composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
License
The MIT License (MIT). Please see License File for more information.
lenny4/doctrine-merge-persistent-collection-bundle 适用场景与选型建议
lenny4/doctrine-merge-persistent-collection-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.29k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2022 年 08 月 27 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「doctrine」 「api-platform」 「Lenny4」 「doctrine-merge-persistent-collection-bundle」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 lenny4/doctrine-merge-persistent-collection-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 lenny4/doctrine-merge-persistent-collection-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 lenny4/doctrine-merge-persistent-collection-bundle 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Common classes for Symfony integration.
Sylius backend integration for Vue Storefront 2
CQRS & Event Sourcing framework
Make pimcore migration simple
Compress integer into alphanumeric
Switch string between (camel case, snake case, kebab case, pascal case, upper case)
统计信息
- 总下载量: 1.29k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 12
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-08-27