首页 > 解决方案 > 在 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';
}

}

标签: symfonyentity

解决方案


推荐阅读