widefocus / magento-zend-hydrator-adapter
一个使用 Zend Hydrator 实现的桥梁,代替 Magento2 实现。
1.1.0
2017-05-13 10:42 UTC
Requires
- php: ^7.0
- magento/framework: ^100.1
- zendframework/zend-filter: ^2.4
- zendframework/zend-servicemanager: ^2.4
- zendframework/zend-stdlib: ^2.4
Requires (Dev)
- phpunit/phpunit: @stable
This package is not auto-updated.
Last update: 2024-09-28 23:38:26 UTC
README
一个适配器,用于使用 Zend Hydrator 实现代替 Magento2 实现。
安装
使用 composer 安装此包。
$ composer require widefocus/magento-zend-hydrator-adapter
使用方法
Magento 2.1 中的 hydrator 实现在不包含任何对象的模型上运行良好。但是,当模型使用例如 DateTimeInterface
时,会抛出异常。
此包通过使 Zend 框架 hydrator 可用来解决此问题。
待 hydrate 的示例类
<?php namespace Foo\Model; use Foo\Api\Data\FooModelInterface; class FooModel implements FooModelInterface { private $date; public function setDate(DateTimeInterface $date) { $this->date = $date; } public function getDate(): DateTimeInterface { return $this->date; } }
DI 配置(etc/di.xml)
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <virtualType name="FooHydratorDateTimeStrategy" type="Zend\Stdlib\Hydrator\Strategy\DateTimeFormatterStrategy"> <arguments> <argument name="format" xsi:type="string">Y-m-d H:i:s</argument> </arguments> </virtualType> <virtualType name="FooModelHydrator" type="WideFocus\Magento\ZendHydratorAdapter\ReflectionHydratorAdapter"> <arguments> <argument name="strategies" xsi:type="array"> <item name="date" xsi:type="object">FooHydratorDateTimeStrategy</item> </argument> </arguments> </virtualType> <type name="Magento\Framework\EntityManager\HydratorPool"> <arguments> <argument name="hydrators" xsi:type="array"> <item name="Foo\Api\Data\FooModelInterface" xsi:type="string">FooModelHydrator</item> </argument> </arguments> </type> </config>
hydrator 现在将由 magento 使用,例如在使用 Magento Entity Manager 时。
以下展示了如何从 hydrator 池中获取 hydrator。
<?php namespace Foo\Controller; use Magento\Framework\EntityManager\HydratorPool; use Foo\Api\Data\FooModelInterface; use Foo\Model\FooModel; class Save { private $hydratorPool; public function __construct(HydratorPool $hydratorPool) { $this->hydratorPool = $hydratorPool; } public function execute() { $model = new FooModel(); $this->hydratorPool ->getHydrator(FooModelInterface::class) ->hydrate($model, ['date' => '2025-12-31 23:59:59']); } }