Autowire doesn't seems to be working

2019-08-21 20:55发布

[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 AppBundle\Form\VarianteEscalierOptGc\VarianteEscalierOptGcEditPoteauType::__construct() must be an instance of AppBundle\Form\DataTransformer\VarianteEscalierTransformer, 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: AppBundle\Listener\ProjetListener
        arguments: ['@security.token_storage']
        tags:
            - { name: doctrine.orm.entity_listener, lazy: true }
    listener.variante:
        class: AppBundle\Listener\VarianteListener
        tags:
            - { name: doctrine.orm.entity_listener, lazy: true }
    service.upload:
        public: true
        class: AppBundle\Service\UploadService
        arguments:
            $dirPicto: '%dir_picto%'

1条回答
Luminary・发光体
2楼-- · 2019-08-21 21:24

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

查看更多
登录 后发表回答