Autowire 似乎不起作用
Autowire doesn't seems to be working
[问题]
我使用的是 Symfony 3.4,现在处理服务的方式有些问题。即使它有效,我也不得不使用旧方法,这是一个问题。
我在表单上使用 DataTransformer,但由于某些原因,出现以下错误
Type error: Argument 1 passed to AppBundle\Form\VarianteEscalierOptGc\VarianteEscalierOptGcEditPoteauType::__construct() must be an instance of AppBundle\Form\DataTransformer\VarianteEscalierTransformer, none given
如文档中所写:
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.
这是我的情况,但仍然出现错误。
此外,如果有人能在下面提示我如何正确更新我的服务,那就太好了。
[文件]
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%'
您缺少服务发现部分,所以在 _default
之后您忘记了这个
_defaults:
...
App\: #You might need to change this to the correct namespace
resource: '../src/*'
[问题]
我使用的是 Symfony 3.4,现在处理服务的方式有些问题。即使它有效,我也不得不使用旧方法,这是一个问题。
我在表单上使用 DataTransformer,但由于某些原因,出现以下错误
Type error: Argument 1 passed to AppBundle\Form\VarianteEscalierOptGc\VarianteEscalierOptGcEditPoteauType::__construct() must be an instance of AppBundle\Form\DataTransformer\VarianteEscalierTransformer, none given
如文档中所写:
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.
这是我的情况,但仍然出现错误。
此外,如果有人能在下面提示我如何正确更新我的服务,那就太好了。
[文件]
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%'
您缺少服务发现部分,所以在 _default
之后您忘记了这个
_defaults:
...
App\: #You might need to change this to the correct namespace
resource: '../src/*'