我得到以下错误:
Type error: Argument 2 passed to DocumentBundleFormSelectionType::DocumentBundleForm{closure}() must be an instance of ReferentialBundleEntityChannel1 or null, instance of DoctrineCommonCollectionsArrayCollection given, called in /srv/http/sp/src/DocumentBundle/Form/SelectionType.php on line 133
不幸的是,我在FormType中没有看到我的错误。
看起来是这样的:
class SelectionType extends AbstractType {
protected $em;
public function __construct(DoctrineORMEntityManager $em)
{
$this->em = $em;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('channel1s', EntityType::class, array(
'class' => 'ReferentialBundle:Channel1',
'property' => 'name',
'label' => 'label.channel1s',
'empty_value' => 'label.select_channel1s',
'mapped' => false,
'expanded' => false,
'translation_domain' => 'UploadProfile',
'multiple' => true,
'required' => false,
));
$formModifier = function (FormInterface $form, Channel1 $channel1s = null) {
$channel3s = null === $channel1s ? array() : $channel1s->getChannel3s();
$form
->add('channel3s', EntityType::class, array(
'class' => 'ReferentialBundle:Channel3',
'choices' => $channel3s,
));
};
$builder->addEventListener(
FormEvents::PRE_SET_DATA,
function (FormEvent $event) use ($formModifier) {
$data = $event->getData();
$formModifier($event->getForm(), $data->getChannel1s()[0]);
}
);
$builder->get('channel1s')->addEventListener(
FormEvents::POST_SUBMIT,
function (FormEvent $event) use ($formModifier) {
$channel1s = $event->getForm()->getData();
$formModifier($event->getForm()->getParent(), $channel1s);
}
);
$builder
->add('agencies', EntityType::class, array(
'class' => 'AppBundle:Agency',
'property' => 'agent_name',
'label' => 'label.agencies',
'empty_value' => 'label.select_agency',
'expanded' => false,
'translation_domain' => 'UploadProfile',
'multiple' => true,
));
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'DocumentBundleEntityUploadProfile',
'em' => null,
));
}
public function getName()
{
return 'uploadprofile';
}
}
错误是针对此行中的$channel1s变量:
$formModifier($event->getForm()->getParent(), $channel1s);
Channel1与Uploadprofile实体具有ManyToMany关系。
编辑
这是转储:
ArrayCollection {#1828 ▼
-elements: array:1 [▼
0 => Channel1 {#1812 ▼
#id: "C"
#translationKey: "name.c"
#name: "Consolidator"
-uploadProfiles: PersistentCollection {#1895 ▼
-snapshot: []
-owner: Channel1 {#1812}
-association: array:16 [ …16]
-em: EntityManager {#456 …11}
-backRefFieldName: "channel1s"
-typeClass: ClassMetadata {#1810 …}
-isDirty: false
#collection: ArrayCollection {#1896 ▼
-elements: []
}
#initialized: false
}
#channel3s: PersistentCollection {#1879 ▼
-snapshot: []
-owner: Channel1 {#1812}
-association: array:15 [ …15]
-em: EntityManager {#456 …11}
-backRefFieldName: "channel1"
-typeClass: ClassMetadata {#1748 …}
-isDirty: false
#collection: ArrayCollection {#1878 ▼
-elements: []
}
#initialized: false
}
}
]
}
您必须将一个Channel1
对象传递给formModifier,这里传递的是$event->getForm()->getData()
,它不是channel1类型的
要从arraycollection获取对象channel1,必须替换以下代码:
channel1s = $event->getForm()->getData();
$formModifier($event->getForm()->getParent(), $channel1s);
通过
channel1s = $event->getForm()->getData()->first();
$formModifier($event->getForm()->getParent(), $channel1s);