Multiple (oneToMany) Entities form generation with symfony2 and file upload

最后都变了- 提交于 2019-12-24 05:03:52

问题


I'm on a problem related to a Symfony2 application which i'm building. The problem concerns an article (News) linked to one or many pictures (Illustration). It seems pretty simple. But i'm stacking on the controller which should persist the News, the Illustration and uploading the picture file.

My News form type:

namespace Fcbg\NewsBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;

class NewsType extends AbstractType
{
    public function buildForm( FormBuilderInterface $builder, array $options )
    {
        $builder
            ->add('date', 'date')
            ->add('titre', 'text')
            ->add('contenu', 'textarea')
            ->add('publication', 'checkbox', array('required' => false))
            ->add('type', 'entity', array( 'class' => 'FcbgNewsBundle:Illustration', 'property' => 'value', 'multiple' => true ));
    }

    public function getName()
    {
        return 'News';
    }
}

My picture(s) form type:

namespace Fcbg\NewsBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class IllustrationType extends AbstractType
{
    public function buildForm( FormBuilderInterface $builder, array $options )
    {
        $builder
            ->add('file', 'file');
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Fcbg\NewsBundle\Entity\Illustration',
            'cascade_validation' => true,
        ));
    }
    public function getName()
    {
        return 'News';
    }
}

My controller action:

public function addAction()
    {
       //link works properly I think
       $news = new News();
       $illustration = new Illustration();
       $illustration->setNews($news);
       $news->addIllustration($illustration);

       $form = $this->createForm(new NewsType(), $news);
       $request = $this->get('request');

       if ($request->getMethod() == 'POST') {
          $form->bind($request);
          if ($form->isValid()) {
             $doctrine = $this->getDoctrine();
             $newsManager = $doctrine->getManager();

             $newsManager->persist($news);
             $newsManager->persist($illustration);
             $newsManager->flush();

             return $this->redirect(...);
          }
        }

        return $this->render('base.html.twig', 
            array(
                'content' => 'FcbgNewsBundle:Default:formulaireNews.html.twig',
                'form' =>  $form->createView(),
                'name' => "add a news"
            )
        );  
    }

The error i get on the execution:

Entities passed to the choice field must be managed. Maybe persist them in the entity manager?

The problem here is that my entity gets a method called "getIllustrations()" which returns logically an arrey of Illustration. So i'm not able to understand this error/question. I assume that my "illustration should be a file field and not a choice field...

Any idea about how i can go further? thx a lot!


回答1:


I think the problem is that you are using the 'entity' form field here:

->add('type', 'entity', array( 'class' => 'FcbgNewsBundle:Illustration', 'property' => 'value', 'multiple' => true ));

and this type of form field acts as a choice and it's used to work with elements created in the database. You can see this in http://symfony.com/doc/current/reference/forms/types/entity.html

A possible solution could be use the "prototype" like here http://symfony.com/doc/current/cookbook/form/form_collections.html#cookbook-form-collections-new-prototype

where you could have:

public function buildForm( FormBuilderInterface $builder, array $options )
{
    $builder
        ->add('date', 'date')
        ->add('titre', 'text')
        ->add('contenu', 'textarea')
        ->add('publication', 'checkbox', array('required' => false))
        ->add('type', 'collection', array(
            'type'         => new IllustrationType(),
            'allow_add'    => true,
    ));
}

I hope that this be useful for you.

Kind regards.




回答2:


So, the code which I was talking about:

public function buildForm( FormBuilderInterface $builder, array $options )
    {
        $builder
            ->add('date', 'date')
            ->add('titre', 'text')
            ->add('contenu', 'textarea')
            ->add('publication', 'checkbox', array('required' => false))
            ->add('illustrations', 'collection', array(
                'type' => new IllustrationType(), 
                'allow_add' => true 
            ));
    }


来源:https://stackoverflow.com/questions/23456108/multiple-onetomany-entities-form-generation-with-symfony2-and-file-upload

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!