具有一个实体的所有行的一种形式 [英] One form with all row of one entity

查看:57
本文介绍了具有一个实体的所有行的一种形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有3个字符串字段的实体Film. 我已经制作了一个表格,可以毫无问题地创建实体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'
    ));
}

解决方案

我已经找到了!!!!

这是我的解决方案,如果有人需要解决相同的问题.

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

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'
    ));
}

解决方案

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

这篇关于具有一个实体的所有行的一种形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆