Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
428 views
in Technique[技术] by (71.8m points)

symfony - One form with all row of one entity

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:FilmFilm')->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' => 'xxxxBundleEntityFilmFilm'
        ));
    }
}

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à ...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...