使用symfony2生成多个(oneToMany)实体表单并上传文件 [英] Multiple (oneToMany) Entities form generation with symfony2 and file upload

查看:125
本文介绍了使用symfony2生成多个(oneToMany)实体表单并上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理与我正在构建的Symfony2应用程序有关的问题.问题与链接到一张或多张图片(插图)的文章(新闻)有关.看起来很简单.但是我正在控制器上堆叠,该控制器应保留新闻,插图并上传图片文件.

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.

我的新闻"表单类型:

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

我的照片表格类型:

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

我的控制器动作:

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

执行时出现错误:

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

这里的问题是我的实体获得了一个名为"getIllustrations()"的方法,该方法在逻辑上返回插图"的格式.因此,我无法理解此错误/问题.我假设我的插图应该是文件字段,而不是选择字段...

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!

推荐答案

我认为问题在于您在此处使用实体"表单字段:

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

和这种类型的表单字段用作选择,它用于处理在数据库中创建的元素.您可以在 http://symfony.com/doc/current/中看到reference/forms/types/entity.html

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

可能的解决方案是使用原型",例如 http://symfony.com/doc/current/cookbook/form/form_collections.html#cookbook-form-collections-new-prototype

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

您可能拥有的地方:

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

我希望这对您有用.

亲切的问候.

这篇关于使用symfony2生成多个(oneToMany)实体表单并上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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