Php 为集合类型创建数据数组
我正在做一个基于Symfony的项目,我正在使用FosUserBundle 我有一个问题,当试图呈现一个Php 为集合类型创建数据数组,php,forms,symfony,model-view-controller,fosuserbundle,Php,Forms,Symfony,Model View Controller,Fosuserbundle,我正在做一个基于Symfony的项目,我正在使用FosUserBundle 我有一个问题,当试图呈现一个CollectionType:Class时,我想让我的用户用数据库中没有的内容填充字段 从我在Symfony文档中读到的内容来看,为了使该类型工作,有必要预先设置一个数据数组作为条目 所以我的问题是,“我是否需要创建一个数据数组并将其传递给我的字段以使其显示?” 我的工作区类型: class WorkshopType extends AbstractType implements Contai
CollectionType:Class
时,我想让我的用户用数据库中没有的内容填充字段
从我在Symfony文档中读到的内容来看,为了使该类型工作,有必要预先设置一个数据数组作为条目
所以我的问题是,“我是否需要创建一个数据数组并将其传递给我的字段以使其显示?”
我的工作区类型:
class WorkshopType extends AbstractType implements ContainerAwareInterface
{
use ContainerAwareTrait;
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add( 'title', TextType::class, array('label'=>"Titre"))
->add( 'goal', TextareaType::class, array('label'=>"But"))
->add( 'theme', TextType::class, array('label'=>"Theme"))
->add( 'target', TextType::class, array('label'=>"Cible"))
->add( 'summary', TextareaType::class, array('label'=>"Résumé"))
->add('maxParticipants', NumberType::class, array('label'=>"Nombre de Participants Maximum", 'data' => 0))
->add('moderatorsNumber', NumberType::class, array('label'=>"Nombre de Modérateurs",'data' => 0))
->add('materials', CollectionType::class, [
//'label' => 'Matériaux',
'entry_type' => TextType::class,
'entry_options' => array(
'attr' => array('class' => 'material-box')
),
'prototype' => true,
'allow_add' => true,
'allow_delete' => true,
])
->add( 'link', UrlType::class, array('label'=>"Lien"))
->add('video', FileType::class, array('label'=>"Ajouter une vidéo", "data_class" => null))
->add('image', FileType::class, array('label'=>"Ajouter une image", "data_class" => null))
->add('document', FileType::class, array('label'=>"Ajouter un document", "data_class" => null))
->add('tags', EntityType::class, [
'class' => Tag::class,
'choice_label' => 'name',
'multiple' => true,
'expanded' => true,
'empty_data' => 'Aucun',
'label' => 'Ajouter des affinités',
])
->add('duration', TimeType::class, array(
'label' => 'Durée',
'input' => 'timestamp',
'widget' => 'choice',
))
->add('preparationTime', TimeType::class, array(
'label' => 'Temps de préparation',
'input' => 'timestamp',
'widget' => 'choice',
))
;
// It's here you have to make changes if you want to change the duration render type
$builder
->get('duration')->addModelTransformer(new SecondDurationDataTransformer());
$builder
->get('preparationTime')->addModelTransformer(new SecondDurationDataTransformer());
$builder
->get('document')->addModelTransformer(new MediaTransformer(
"ApiBundle\Entity\Workshop",
$options['entityId'],
$this->container->get('doctrine.orm.entity_manager'),
$this->container->get('bnpp.api.file_manager.local'),
"document",
"ApiBundle\Entity\Document"));
$builder
->get('image')->addModelTransformer(new MediaTransformer(
"ApiBundle\Entity\Workshop",
$options['entityId'],
$this->container->get('doctrine.orm.entity_manager'),
$this->container->get('bnpp.api.file_manager.local'),
"image",
"ApiBundle\Entity\Image"));
$builder
->get('video')->addModelTransformer(new MediaTransformer(
"ApiBundle\Entity\Workshop",
$options['entityId'],
$this->container->get('doctrine.orm.entity_manager'),
$this->container->get('bnpp.api.file_manager.local'),
"video",
"ApiBundle\Entity\Video"));
}
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Workshop::class,
'required' => false,
"entityId" => null,
]);
}
/**
* {@inheritdoc}
*/
public function getBlockPrefix()
{
return 'apibundle_workshop';
}
}
谢谢大家,您可以将数组作为默认值传递,如下所示:
'choices'=>$yourArray,