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
*
* @ORMColumn(name="id", type="integer")
* @ORMId
* @ORMGeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORMColumn(type="string",nullable=false)
*/
private $name;
/**
*
* @ORMColumn(type="string")
*/
private $style;
/**
* @ORMColumn(type="string")
*/
private $director;
/**
* @ORMColumn(type="string")
*/
private $actor;
Controller Film
public function updateAction(Request $request)
{
$films = $this->getDoctrine()
->getRepository('LfayBundle:FilmFilm');
$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' => 'LfayBundleEntityFilmFilm'
));
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…