symfony - 在 createForm (Symfony 2) 中插入实体
问题描述
我找了几天来找到解决方案。我不能在 createForm 中传递我的实体,但只有当我传递实体特定的 getter 时它才有效
不工作:
$storyboard = $this->getDoctrine()
->getRepository('TutoAppBundle:Storyboard')
->find($id_screen);
$formSettings = $this->createForm( new MosaicCorporateCastType( ), $storyboard );
不工作:
$storyboard = $this->getDoctrine()
->getRepository('TutoAppBundle:Storyboard')
->find($id_screen);
$formSettings = $this->createForm( new MosaicCorporateCastType( ), $storyboard->getPosition() );
在职的:
$storyboard = $this->getDoctrine()
->getRepository('TutoAppBundle:Storyboard')
->find($id_screen);
$formSettings = $this->createForm( new MosaicCorporateCastType( ), $storyboard->getSettings() );
我试图理解为什么......如果你有任何建议,我很高兴。
谢谢您的帮助
这是马赛克CorporateCastType:
类 MosaicCorporateCastType 扩展 AbstractType {
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
/*
* optional title
*/
->add('title', 'text', array(
'attr' => array(
'class' => 'form-control'
),
'label' => 'Titre informationnel de l\'écran',
'mapped' => false
))
/*
* mode la mosaïque
*/
->add('animation', 'choice', array(
'label' => 'Animation de la mosaïque',
'attr' => array(
'class' => 'form-control'
),
'choices' => array(
'fade' => 'fade',
'slide-down' => 'slide-down',
'slide-left' => 'slide-left',
'flip-horizontal' => 'flip-horizontal',
'flip-vertical' => 'flip-vertical'
),
'mapped' => false
))
/*
* Durée avant de passer à l'écran suivant
*/
->add('duration', 'choice', array(
'label' => 'Durée de la mosaïque (secondes)',
'attr' => array(
'class' => 'form-control'
),
'choices' => array(
'5' => '5',
'10' => '10',
'15' => '15',
'20' => '20',
'25' => '25',
'30' => '30',
'35' => '35',
'40' => '40',
'45' => '45',
'50' => '50',
'55' => '55',
'60' => '60'
)
))
/*
* interval entre 2 slides en milliseconde
*/
->add('intervalTime', 'choice', array(
'label' => 'Intervalle entre deux slides (millisecondes)',
'attr' => array(
'class' => 'form-control'
),
'choices' => array(
'1000' => '1OOO',
'2000' => '2000',
'3000' => '3000',
'4000' => '4000',
'5000' => '5000',
'6000' => '6000',
'7000' => '7000',
'8000' => '8000',
'9000' => '9000',
'10000' => '10000'
)
))
/*
* style du picto
*/
-> add('iconType', 'choice', array(
'label' => 'Choix du pictogramme',
'attr' => array(
'class' => 'form-control'
),
'choices' => array(
'ob' => 'ob',
'bubble' => 'bubble',
'sq' => 'sq',
'default' => 'default'
)
))
/*
* background color (box)
*/
->add('backgroundColor', 'collection', array(
'type' => new BackgroundColorContentType(),
'allow_add' => true,
'allow_delete' => true,
'prototype' => true
))
/*
* all screens
*/
->add('source', 'choice', array(
'label' => 'Type de sources',
'attr' => array(
'class' => 'form-control'
),
'choices' => array(
'twitter' => 'Twitter',
'instagram' => 'Instagram',
'facebook' => 'Facebook'
),
'required' => true,
))
;
// pour récupérer les informations depuis la base de données
$builder->addEventListener(FormEvents::POST_SET_DATA, array($this, 'onPostSetData'));
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'OuibeatAppBundle:Storyboard',
));
}
public function onPostSetData(FormEvent $event) {
$form = $event->getForm();
$settings = $event->getData();
if($settings) {
foreach ((array)$settings as $name => $value) {
$form->get($name)->setData($value);
}
}
}
public function getName()
{
return 'mosaic';
}
}
解决方案
推荐阅读
- python - How to import python packages while using it in node.js application in VS code
- python - 使用python格式化日期时间格式的正则表达式
- powerbi - DAX DEFINE 返回未找到列错误
- django - 草稿帖子:自定义管理器仅提取已发布的帖子,但在管理站点中我需要所有帖子
- javascript - 单击链接,图像向下滚动,一些信息以javascript显示
- google-cloud-platform - 我的 GCP 项目自动创建了 2 个存储桶
- java - 在Angular(Spring API)中刷新浏览器时如何避免重新加载表数据
- arcore - 如何在 LG V30 中使用 AR Core?
- python - MFCCs 数组中的第二个数字是多少?
- c# - 我需要帮助将日期和时间转换为单独日期