forms - 自动接线似乎不起作用
问题描述
[问题]
我在 Symfony 3.4 上,现在处理服务的方式遇到了一些问题。即使它正在工作,我也被迫使用旧方法,这是一个问题。
我在表单上使用DataTransformer,但由于某些原因,出现以下错误
类型错误:参数 1 传递给 AppBundle\Form\VarianteEscalierOptGc\VarianteEscalierOptGcEditPoteauType::__construct() 必须是 AppBundle\Form\DataTransformer\VarianteEscalierTransformer 的实例,没有给出
正如文档中所写:
而已!只要你使用 autowire 和 autoconfigure,Symfony 就会自动知道向你的 TaskType 传递一个 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/*'
推荐阅读
- kubernetes - 如何为 Kubernetes 准入控制器设置速率限制?
- angular - 我想在我的表单中重置进度/角剑道编辑器控件
- python - 对数组进行降序和升序求和时的 2 种不同结果
- javascript - 如何从 Solana 的助记词中获取正确的公共地址?
- python - 单击破折号引导组件中的 html.Progress 栏以重定向到另一个页面
- ios - 如何在本地通知中实现深层链接
- django - 对象创建根据基本模型字段分配代理类
- c - 使用 LD_PRELOAD 包装克隆系统调用
- java - 使用代码不断使索引 1 超出长度 1 错误的范围
- google-apps-script - 有没有办法使用 Google 应用程序脚本查看网站网络请求的名称?