Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
244 views
in Technique[技术] by (71.8m points)

forms - Autowire doesn't seems to be working

[PROBLEM]

I'm on Symfony 3.4, and I got some issues with the way services are handled now. Even though it's working, I'm forced to use the old way, which is a problem.

I'm using DataTransformer on a form, but for some reasons, I got the following error

Type error: Argument 1 passed to AppBundleFormVarianteEscalierOptGcVarianteEscalierOptGcEditPoteauType::__construct() must be an instance of AppBundleFormDataTransformerVarianteEscalierTransformer, none given

As written in the doc:

That's it! As long as you're using autowire and autoconfigure, Symfony will automatically know to pass your TaskType an instance of the IssueToNumberTransformer.

Which is my case, but still got the error.

Also, if someone can hint me how to correctly update my services below, that would be great.

[FILES]

FormType

public function buildForm(FormBuilderInterface $builder, array $options) {
    $builder->add('varianteEscalier', HiddenType::class, array('data'=>$options['data']->getVarianteEscalier()->getId()))
            ->add('gardecorpsOption', EntityType::class, array(
                'class'=>'AppBundle:GardecorpsOption',
                'query_builder'=>function(EntityRepository $er) {
                    return $er->createQueryBuilder("gco")
                              ->where("gco.type='poteau'")
                              ->andWhere("gco.actif=1");
                },
            ))
            ->add('quantite');
    $builder->get('varianteEscalier')->addModelTransformer($this->transformer);
}

Transformer.php

class VarianteEscalierTransformer implements DataTransformerInterface {
    private $em;

    /**
     * @param EntityManagerInterface $em
     */
    public function __construct(EntityManagerInterface $em) {
        $this->em=$em;
    }

    /**
     * @param  Object|null $entity
     * @return string
     */
    public function transform($entity) {
        if(null === $entity) {
            return "";
        }

        return $entity->getId();
    }

    /**
     * @param $entityId
     * @return VarianteEscalier|null
     */
    public function reverseTransform($entityId) {
        if(!$entityId) {
            return null;
        }
        $entity=$this->em->getRepository(VarianteEscalier::class)->findOneBy(array('id'=>$entityId));
        if($entity === null) {
            throw new TransformationFailedException(sprintf('VarianteEScalier avec l'id '.$entityId.' n'existe pas!'));
        }

        /** @noinspection PhpIncompatibleReturnTypeInspection */
        return $entity;
    }
}

services.yml

services:
    _defaults:
        autowire: true
        autoconfigure: true
        public: false
    listener.projet:
        class: AppBundleListenerProjetListener
        arguments: ['@security.token_storage']
        tags:
            - { name: doctrine.orm.entity_listener, lazy: true }
    listener.variante:
        class: AppBundleListenerVarianteListener
        tags:
            - { name: doctrine.orm.entity_listener, lazy: true }
    service.upload:
        public: true
        class: AppBundleServiceUploadService
        arguments:
            $dirPicto: '%dir_picto%'
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You're missing service discovery section, so right after _default you forgot this

_defaults:
  ...

  App: #You might need to change this to the correct namespace
    resource: '../src/*'

Take a look https://symfony.com/doc/current/service_container/3.3-di-changes.html#step-4-auto-registering-services


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...