我有一个包含3个字符串字段的实体电影。 我已经创建了一个没有问题的创建实体A的表单,它很好。
现在我想在我的实体中逐个制作一个表格(逐行),并且能够更改我想要的字段,并一次性保存所有更改。
我尝试使用烹饪书“如何嵌入表单集合”,但它与我的问题不符:c /
以下是一些示例代码,用于解释我正在尝试做什么,但是哪些不起作用:
实体电影
class Film
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\Column(type="string",nullable=false)
*/
private $name;
/**
*
* @ORM\Column(type="string")
*/
private $style;
/**
* @ORM\Column(type="string")
*/
private $director;
/**
* @ORM\Column(type="string")
*/
private $actor;
控制器电影
public function updateAction(Request $request)
{
$films = $this->getDoctrine()
->getRepository('LfayBundle:Film\Film');
$films_all = $films->findAll();
foreach ($films_all as $film) {
$form = $this->createForm(FilmType::class, $film);
$forms[] = $form->createView();
}
$form_film->handleRequest($request);
if ($form_film->isSubmitted() && $form_film->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($films_all);
$em->flush();
return $this->redirectToRoute('film_update');
}
return $this->render(
'film/film_update.html.twig',
array(
'form_film' => $forms,
)
);
}
类型电影
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', TextType::class)
->add('style', TextType::class)
->add('director', TextType::class)
->add('actor', TextType::class)
;
}
/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'LfayBundle\Entity\Film\Film'
));
}
答案 0 :(得分:2)
我找到了!!!!
如果有人需要解决同样的问题,这是我的解决方案。
FilmController:
/**
* @Route("/film/update", name="film_update")
*/
public function updateAction(Request $request)
{
$films = array('films' => $this->getDoctrine()->getRepository('LfayBundle:Film\Film')->findAll()) ;
$forms = $this->createForm(FilmContainerType::class, $films);
$forms->handleRequest($request);
if ($forms->isSubmitted() && $forms->isValid()) {
$em = $this->getDoctrine()->getManager();
//$em->persist($films_all);
$em->flush();
return $this->redirectToRoute('film_update');
}
return $this->render(
'film/film_update.html.twig',
array(
'forms' => $forms->createView(),
)
);
}
FilmContainerType:
class FilmContainerType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('films', CollectionType::class, array(
'entry_type' => FilmType::class));
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => null
));
}
public function getName()
{
return 'xxxx_bundle_film_container_type';
}
}
FilmType:
class FilmType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('style', TextType::class)
->add('director', TextType::class)
->add('actor', TextType::class)
;
}
/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'xxxxBundle\Entity\Film\Film'
));
}
}
Film_update.html.twig
{% block content %}
<div class="row">
<div class="col-sm-12">
<div class="box box-solid box-primary">
<div class="box-header with-border">
<h3 class="box-title">Liste des applications</h3>
<div class="box-tools pull-right">
<button class="btn btn-box-tool" data-widget="collapse"><i class="fa fa-minus"></i></button>
</div><!-- /.box-tools -->
</div><!-- /.box-header -->
<div class="box-body">
<div class="dataTables_wrapper form-inline dt-bootstrap">
{{ form_start(forms) }}
<table class="table table-bordered table-hover dataTable" id="all_applications" width="100%">
<thead>
<tr>
<th>Film</th>
<th>acteur</th>
<th>realisateur</th>
<th>autre</th>
</tr>
</thead>
<tbody>
{% for film in forms.films %}
{{ form_errors(film) }}
<tr>
<td>{{ film.vars.value.name }}</td>
<td>{{ form_widget(film.style, {'attr': {'class': 'c-select'}}) }}</td>
<td>{{ form_widget(film.director, {'attr': {'class': 'c-select'}}) }}</td>
<td>{{ form_widget(film.actor, {'attr': {'class': 'c-select'}}) }}</td>
</tr>
{% endfor %}
</tbody>
</table>
<button type="submit" class="btn btn-primary btn-lg btn-block">Modifier</button>
{{ form_end(forms) }}
<div>
</div>
</div>
</div>
</div>
</div>
</div><!-- row -->
{% endblock %}
etvoilà...