英文:
TYPO3: string[] to Object[] Type Converter?
问题
现在我有一个类型转换器,从string
转换为我的User::class
,设置如下。顺便说一下,我只是为了一些特定的用户对象使用这个User类,我不希望将它们持久化,这就是为什么它只是反序列化它们:
class UserConverter extends AbstractTypeConverter
{
protected $priority = 1;
protected $sourceTypes = ['string'];
protected $targetType = User::class;
public function convertFrom($source, string $targetType,
array $convertedChildProperties = [],
PropertyMappingConfigurationInterface $configuration = null)
{
return unserialize($source);
}
}
注册后,它会按预期工作,例如,从Fluid表单到控制器操作,序列化的对象会自动反序列化。
这适用于单个属性,比如$report->user
。我正在尝试使用数组属性实现相同的效果,例如$report->users
。所以我创建了:
class UserArrayConverter extends AbstractTypeConverter
{
protected $priority = 1;
protected $sourceTypes = ['array'];
protected $targetType = 'array';
public function convertFrom($source, string $targetType,
array $convertedChildProperties = [],
PropertyMappingConfigurationInterface $configuration = null)
{
return ['TODO'];
}
}
但是在调试TODO行时,我发现执行线程从未达到那里。当然,我得到的是$report->users
作为字符串数组(序列化对象)而不是Users数组。
我应该如何创建TypeConverter,以便将字符串数组转换为Users数组?
TYPO3: 10.4, PHP: 7.4
非常感谢!
英文:
Right now I have a Type Converter, from string
to my User::class
set up like this. By the way, I simply use this for a User class that I have for some specific User objects which I don't want to persist, that's why it simply deserializes them:
class UserConverter extends AbstractTypeConverter
{
protected $priority = 1;
protected $sourceTypes = ['string'];
protected $targetType = User::class;
public function convertFrom($source, string $targetType,
array $convertedChildProperties = [],
PropertyMappingConfigurationInterface $configuration = null)
{
return unserialize($source);
}
}
And after registering it, it works as expected, e.g. from Fluid form to a Controller action the serialized object gets deserialized automatically.
This works for single properties such as for $report->user
. I'm trying to achieve the same with array properties, e.g. $report->users
. So I made:
class UserArrayConverter extends AbstractTypeConverter
{
protected $priority = 1;
protected $sourceTypes = ['array'];
protected $targetType = 'array';
public function convertFrom($source, string $targetType,
array $convertedChildProperties = [],
PropertyMappingConfigurationInterface $configuration = null)
{
return ['TODO'];
}
}
But debuging that TODO line I see that the execution thread never reaches it. And of course I get my $report->users
as an array of strings (serialized objects) instead of an array of Users.
How should I create the TypeConverter so it can convert an array of strings to an array of Users?
TYPO3: 10.4, PHP: 7.4
Thanks a lot in advance!
答案1
得分: 1
你应该使用你的第一个属性映射器,并在initializeAction内部执行魔法。
public function initialize<Whatever>Action()
{
$propertyMapping = $this->arguments
->getArgument('report')
->getPropertyMappingConfiguration();
$propertyMapping->forProperty('users')->allowAllProperties();
$propertyMapping->forProperty('users')->setTypeConverterOptions(
UserConverter::class
);
}
未经测试,但应该是这个方式。
英文:
You should use your first property mapper and do the magic inside initializeAction.
public function initialize<Whatever>Action()
{
$propertyMapping = $this->arguments
->getArgument('report')
->getPropertyMappingConfiguration();
$propertyMapping->forProperty('users')->allowAllProperties();
$propertyMapping->forProperty('users')->setTypeConverterOptions(
UserConverter::class
);
}
Untested, but this should be the way.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论