简体   繁体   中英

Symfony2: How to add FormType

I have an Entity Product and I have an Entity File . The relationship between those 2 is normally a ManyToMany Relation. But I need also a field Value " sorting " and " description ", so I must declare my own JoinTable ProductHasFile . Now I want in my Form, that I can have a Product bute include the File FormTpye an not the ProductHasFile FormType.

Product Entity:

/**
 * @ORM\Entity
 * @ORM\Table(name="Product")
 */
class Product
{

     /**
     * @ORM\OneToMany(targetEntity="ProductHasFile", mappedBy="product", cascade={"persist"}))
     */
     private $productHasFiles;
....

File Entity:

/**
 * @ORM\Entity
 * @ORM\HasLifecycleCallbacks
 */
class File
{
    /**
     * @ORM\OneToMany(targetEntity="ProductHasFile", mappedBy="file")
     */
    private $productHasFiles;
...

And my own generated Entity ProductHasFile:

/**
 * @ORM\Entity
 * @ORM\Table(name="ProductHasFile")
 */
class ProductHasFile
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
    * @ORM\Column(type="integer", nullable=true)
    */
    private $sorting;

    /**
    * @ORM\Column(type="string", nullable=true)
    */
    private $description;

   /**
   * @ORM\ManyToOne(targetEntity="Product", inversedBy="productHasFiles")
   * @ORM\JoinColumn(name="product_id", referencedColumnName="id")
    */
   private $product;

   /**
   * @ORM\ManyToOne(targetEntity="File", inversedBy="productHasFiles")
   * @ORM\JoinColumn(name="file_id", referencedColumnName="id")
   */
   private $file;

When I now make my Formtype for Product:

class ProductType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
           ->add('productHasFiles', CollectionType::class, array(
                    'entry_type' => ProductHasFileType::class,
                    'allow_add' => true,
                    'allow_delete' => true,
                    'by_reference' => false,
                    ,
                )
            )

and my FormType for ProductHasFileType:

class ProductHasFileType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        /**
         * @var $entityManager EntityManager
         */
        $fileRepository = $options['fileRepository'];

        $builder
            ->add('sorting', TextType::class)
            ->add('description', TextType::class)
            ->add('file', EntityType::class, array(
                'class' => 'AppBundle\Entity\File',
                'label' => 'file',
            ))
        ;

I get only a dropdwon for the File Entity. But I want to have my full File FormType which has the Uploadfield and other things, it looks like this:

class FileType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('description', TextType::class, array(
                'label' => 'description',
            ))
            ->add('type', TextType::class, array(
                'label' => 'type',
            ))
          ->add('file', \Symfony\Component\Form\Extension\Core\Type\FileType::class, array(
                    'label' => 'file',

                ))
        ;
    }

Does anybody has a solution for this?

As explained in Symfony doc about how to embed forms this is as easy as

class ProductHasFileType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        /**
         * @var $entityManager EntityManager
         */
        $fileRepository = $options['fileRepository'];

        $builder
            ->add('sorting', TextType::class)
            ->add('description', TextType::class)
            ->add('file', FileType::class) //where FileType is your own FormType
        ;
    }
}

In ProductHasFileType you're adding your file like this:

->add('file', EntityType::class, array(
            'class' => 'AppBundle\Entity\File',
            'label' => 'file',
        ))

so you are adding your file as entity type wich logically renders a dropdown of all file entities.

What you need to do is pass your own FileType .To make sure you are using your own file type and not the default symfony one don't forget to add the correct use statement.

result:

use MyBundle\Form\Type\FileType;

class ProductHasFileType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('sorting', TextType::class)
            ->add('description', TextType::class)
            ->add('file', FileType::class)
        ;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM