Reputation: 422
I have an entity Film with 3 string fields. I've made a form to create Entities A without problems, it woks fine.
Now I would like to make a Table with each films in my entity (one by row) and to be able to change the fields I want to, and save in one time all the changes.
I've try with the cook book "How to Embed a Collection of Forms" but it doesn't correspond to my problem :c/
Here is some sample code to explain what I'm trying to do but who doesn't work :
Entity Film
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;
Controller Film
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,
)
);
}
Type Film
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'
));
}
Upvotes: 1
Views: 792
Reputation: 422
I've found !!!!
Here is my solution if someone need to solve the same problem.
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 %}
et voilà ...
Upvotes: 4