saeven/circlical-collection-hydrator
Composer 安装命令:
composer require saeven/circlical-collection-hydrator
包简介
Hydrator strategy for Doctrine + Laminas Collections.
README 文档
README
This is a collection hydrator that builds on available hydrators, that prevents cases when Doctrine issues duplicate key updates when Laminas Forms and Collections are involved. This issue can be evidenced here
Usage
Installation is very simple, after installing the package with composer, in your form Factory, you will specify the hydrator strategy for your collection to be "CollectionDiffStrategy".
<?php declare(strict_types=1); namespace HydrationTest\Factory\Form; use Doctrine\Laminas\Hydrator\DoctrineObject as DoctrineHydrator; use Doctrine\ORM\EntityManager; use HydrationTest\Form\Hydrator\CollectionDiffStrategy; use HydrationTest\Form\IngredientAmountFieldset; use HydrationTest\Form\RecipeForm; use Laminas\Form\FormElementManager; use Laminas\ServiceManager\Factory\FactoryInterface; use Psr\Container\ContainerInterface; class RecipeFormFactory implements FactoryInterface { public function __invoke(ContainerInterface $container, $requestedName, ?array $options = null) { $hydrator = new DoctrineHydrator($container->get(EntityManager::class), false); $hydrator->addStrategy('ingredient_amounts', new CollectionDiffStrategy(true)); return (new RecipeForm( $container->get(FormElementManager::class)->get(IngredientAmountFieldset::class, $options ?? []), $options ?? [] )) ->setHydrator($hydrator) ->setObject($options['recipe']); } }
Then, in your fieldset factory, you will apply the CollectionComparatorHydrator to return clean objects of the new type.
<?php declare(strict_types=1); namespace HydrationTest\Factory\Form; use Doctrine\ORM\EntityManager; use HydrationTest\Entity\Ingredient; use HydrationTest\Entity\IngredientAmount; use HydrationTest\Form\Hydrator\CollectionComparatorHydrator; use HydrationTest\Form\IngredientAmountFieldset; use Laminas\ServiceManager\Factory\FactoryInterface; use Psr\Container\ContainerInterface; class IngredientAmountFieldsetFactory implements FactoryInterface { public function __invoke(ContainerInterface $container, $requestedName, ?array $options = null) { $recipe = $options['recipe']; /** @var EntityManager $entityManager */ $entityManager = $container->get(EntityManager::class); $collectionHydrator = new CollectionComparatorHydrator($entityManager, function (array $data, object $object) use ($entityManager, $recipe) { $ingredient = $entityManager->getRepository(Ingredient::class)->findOneBy(['id' => $data['ingredient']]); return new IngredientAmount($recipe, $ingredient, $data['tablespoons'] ?? 0); }, false); return (new IngredientAmountFieldset($entityManager, 'ingredient_amounts')) ->setHydrator($collectionHydrator) ->setObject(new IngredientAmount($options['recipe'], null, null)); } }
As last step, we must implement the necessary comparators on the objects that are hydrated by the fieldset; see CollectionDiffInterface.
<?php declare(strict_types=1); namespace HydrationTest\Entity; use Doctrine\ORM\Mapping as ORM; use HydrationTest\Model\CollectionDiffInterface; /** * @ORM\Entity * @ORM\Table(name="recipes_ingredients"); */ class IngredientAmount implements CollectionDiffInterface { /** * @ORM\Id * @ORM\ManyToOne(targetEntity="Recipe", inversedBy="ingredient_amounts") * @ORM\JoinColumn(name="recipe_id", referencedColumnName="id", onDelete="cascade") * * @var Recipe */ private $recipe; /** * @ORM\Id * @ORM\ManyToOne(targetEntity="Ingredient") * @ORM\JoinColumn(name="ingredient_id", referencedColumnName="id", onDelete="cascade") * * @var ?Ingredient */ private $ingredient; /** * @ORM\Column(type="integer", nullable=false, options={"default":0, "unsigned":true}) * * @var ?int */ private $tablespoons; public function __construct(Recipe $recipe, ?Ingredient $ingredient, ?int $tablespoons) { $this->recipe = $recipe; $this->ingredient = $ingredient; $this->tablespoons = $tablespoons; } public function getIngredient(): Ingredient { return $this->ingredient; } public function setTablespoons(int $tablespoons): void { $this->tablespoons = $tablespoons; } public function getTablespoons(): int { return $this->tablespoons; } public function getDiffIdentifier(): string { return $this->recipe->getId() . '-' . $this->ingredient->getId(); } public function copyValuesFrom(object $object): void { if (!$object instanceof IngredientAmount) { return; } $this->tablespoons = $object->getTablespoons(); } }
After these small changes, your collections will no longer issue update statements that cause duplicate keys.
saeven/circlical-collection-hydrator 适用场景与选型建议
saeven/circlical-collection-hydrator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.44k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 09 月 11 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 saeven/circlical-collection-hydrator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 saeven/circlical-collection-hydrator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 1.44k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 14
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-09-11