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!

有帮助吗?

解决方案

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.

其他提示

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 
            ));
    }
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top