我是一個奇怪的問題,我不知道如何解決。 我創建了一個datatransformer,其中除在我的其他項目其他的事情,完美的作品,但是當我開始頁面我得到這個錯誤:DataTransformer得到錯誤「undefined call method getName」
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發生的錯誤?
btw。你目前沒有在form-type中使用entity-manager =>考慮刪除依賴項:)我的答案解決了你的問題。 – nifr