我想使用我的DataTransformer将base64字符串转换为fileSystem图像。
我的代码:
services:
lion_visionomie_media_bundle_base64_data_transformer:
class: Lion\Visionomie\MediaBundle\Transformer\Base64DataTransformer
arguments: ['@sonata.media.generator.default', '@sonata.media.manager.media', '@sonata.media.pool', '@doctrine.orm.entity_manager']
lion_visionomie_media_bundle_slide_type:
class: Lion\Visionomie\MediaBundle\Form\SlideType
arguments: ['@lion_visionomie_media_bundle_base64_data_transformer']
tags:
- {name: form.type}
class Base64DataTransformer implements DataTransformerInterface
{
/**
* @var GeneratorInterface
*/
private $pathGenerator;
/**
* @var MediaManager
*/
private $mediaManager;
/**
* @var Pool
*/
private $pool;
/**
* @var EntityManagerInterface
*/
private $entityManager;
public function __construct(GeneratorInterface $generator, MediaManager $mediaManager, Pool $pool, EntityManagerInterface $entityManager)
{
$this->pathGenerator = $generator;
$this->mediaManager = $mediaManager;
$this->pool = $pool;
$this->entityManager = $entityManager;
}
/**
* @param mixed $value
* @return mixed
*/
public function transform($value)
{
// [...]
return $media;
}
/**
* @param mixed $value
* @return mixed
*/
public function reverseTransform($value)
{
/** @var Media $media */
$media = $value;
return base64_encode(file_get_contents($media->getPreviousProviderReference()));
}
}
class SlideType extends AbstractType
{
/**
* @var Base64DataTransformer
*/
private $base64DataTransformer;
public function __construct(Base64DataTransformer $base64DataTransformer)
{
dump($base64DataTransformer);
die;
$this->base64DataTransformer = $base64DataTransformer;
}
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name')
->add('active')
->add('background')
->addModelTransformer($this->base64DataTransformer)
->add('thumbnail')
;
}
我目前的问题是,SlideType中的base64DataTransformer变量为空。
你能告诉我为什么这是空的吗?
也许我可以在我的SlideType中使用ContainerAwareInterface。但我不想手动将我的依赖项放到我的DataTransformer类中。
答案 0 :(得分:0)
如果您想在DIC中使用Symfony注册表单&lt; 2.8,您应该将alias: <result of getName()>
参数添加到该类型的form.type
标记中。在您的情况下,如果SlideType::getName()
返回slide
,请将标记设置为{ name: form.type, alias: slide }
,并确保通过传递名称而不是类型实例来实例化表单:
// good
$form = $this->createForm('slide', ...);
// bad
$form = $this->createForm(new SlideType(), ...);