DataTransformer获取错误“未定义的调用方法getName”

时间:2013-11-26 17:47:56

标签: symfony symfony-2.3

我是一个奇怪的问题,我不知道如何解决。 我创建了一个datatransformer,其中包括我的其他项目中的其他功能,但是当我启动页面时,我收到了这个错误:

FatalErrorException: Error: Call to undefined method 
Acme\CoreBundle\Transformer\HiddenToIdTransformer::getName() in
/var/www/Acme/vendor/symfony/symfony/src/Symfony/Component/Form/Extension/DependencyInjection/DependencyInjectionExtension.php line 49

这里是代码:

变压器

class HiddenToIdTransformer implements DataTransformerInterface
{
    /**
     * @var ObjectManager
     */
    protected $objectManager;

    /**
     * @var string
     */
    protected $class;

    public function __construct(ObjectManager $objectManager)
    {
        $this->objectManager = $objectManager;
    }

    public function transform($entity)
    {
        if (null === $entity) {
            return;
        }

        return $entity->getId();
    }

    public function reverseTransform($name)
    {
        if (!$name) {
            return null;
        }

        $entity = $this->objectManager
            ->getRepository('AcmeCoreBundle:Locality')
            ->findOneByLocality($name);

        if (null === $entity) {
            throw new TransformationFailedException();
        }

        return $entity;
    }
}

class EntityHiddenType extends AbstractType
{
    /**
     * @var ObjectManager
     */
    protected $objectManager;

    public function __construct(ObjectManager $objectManager)
    {
        $this->objectManager = $objectManager;
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $transformer = new HiddenToIdTransformer($this->objectManager);
        $builder->addModelTransformer($transformer);
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'class' => null,
            'invalid_message' => 'The entity does not exist.',
        ));
    }

    public function getParent()
    {
        return 'hidden';
    }

    public function getName()
    {
        return 'entity_hidden';
    }
}

服务

<service id="datatransformer.entity_hidden" class="Acme\CoreBundle\Transformer\HiddenToIdTransformer">
        <argument type="service" id="doctrine.orm.entity_manager" />
        <tag name="form.type" alias="entity_hidden" />
</service>

以这种方式召回变压器:

->add('locality', 'entity_hidden')

我不明白有什么问题,我在其他项目中重复一遍,完全相同的代码工作正常!

也许这是2.3.7中的错误?

1 个答案:

答案 0 :(得分:2)

您已将Transformer的类配置为使用form.type标记的服务,而不是使用表单类型的类:

<service 
    id="datatransformer.entity_hidden"  
    class="Acme\CoreBundle\Transformer\HiddenToIdTransformer" <!-- <= HERE -->
>

表单类型的服务定义应如下所示:

<service id="form.type.entity_hidden" class="Acme\CoreBundle\Form\Type\EntityHiddenType">
        <argument type="service" id="doctrine.orm.entity_manager" />
        <tag name="form.type" alias="entity_hidden" />
</service>

如果您希望您的数据转换器也是服务...不要用form.type标记它,否则symfony会尝试调用它不存在的getName()方法。

删除标签......

<tag name="form.type" alias="entity_hidden" />

......异常将消失。