Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

[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

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

1 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...