【问题标题】:Symfony Forms and ManyToMany. How to configure form with file upload field that is also EntityType field?Symfony 表单和多对多。如何使用也是 EntityType 字段的文件上传字段配置表单?
【发布时间】:2018-04-26 07:55:51
【问题描述】:

我需要制作带有文件上传的表单字段,这也是 ManyToMany 实体的一部分。现在我的配置如下所示,并且可以正常工作...

class ProductTypeNew extends AbstractType
{
    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name')
            ->add('price')
            ->add('description', TextareaType::class)
            ->add('quantity')
            ->add('file', FileType::class, array('label' => 'Zdjęcie'))
        ;

...但我需要在控制器中手动获取表单输入并设置为表单实体

if ($form->isSubmitted() && $form->isValid())
{
    $image = new ShopProductImages();

    $file = $product->getFile();

    $fileName = $this->generateUniqueFileName().'.'.$file->guessExtension();

    $file->move(
        $this->getParameter('shop_images_directory'),
        $fileName
    );

    $image->setFile($fileName);
    $product->addShopProductImages($image);
    $product->setFile($fileName);

    $em = $this->getDoctrine()->getManager();
    $em->persist($image);
    $em->persist($product);
    $em->flush();

我想做这样的事情(但它不起作用):

        ->add('shopProductImages', EntityType::class, array(
                'by_reference' => false,
                'entry_type' => FileType::class,
            )

带有嵌入表单的新版本表单类型也会导致问题:

类型“Doctrine\Common\Collections\Collection|array”的预期值 对于关联领域 “AppBundle\Entity\ShopProducts#$shopProductImages”,得到 改为“Symfony\Component\HttpFoundation\File\UploadedFile”。

...具有以下配置:

产品类型新:

class ProductTypeNew extends AbstractType
{
    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name', null, array('label' => 'Nazwa'))
            ->add('price', null, array('label' => 'Cena'))
            ->add('description', TextareaType::class, array('label' => 'Opis'))
            ->add('quantity', null, array('label' => 'Ilość'))
            ->add('shopProductImages', ShopProductsImagesType::class);
    }

    /**
     * {@inheritdoc}
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => ShopProducts::class,
        ]);
    }

ShopProductsImagesType:

class ShopProductsImagesType extends AbstractType
{
    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('file', FileType::class, array('label' => 'Zdjęcie'))
        ;
    }

    /**
     * {@inheritdoc}
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
//            'data_class' => ShopProductImages::class,
            'data_class' => null,
        ]);
    }

实体店产品:

/**
 * ShopProducts
 *
 * @ORM\Table(name="shop_products")
 * @ORM\Entity
 */
class ShopProducts
{
   ....

    /**
     * INVERSE SIDE
     *
     * @var \Doctrine\Common\Collections\Collection
     *
     * @ORM\ManyToMany(
     *     targetEntity="AppBundle\Entity\ShopProductImages",
     *     mappedBy="shopProducts",
     *     cascade={"persist"}
     * )
     */
    private $shopProductImages;

实体店ProductImages:

* @ORM\Entity
 */
class ShopProductImages
{
    /**
     * @var string
     *
     * @ORM\Column(name="file", type="text", length=255, nullable=true)
     */
    private $file;

【问题讨论】:

    标签: php symfony doctrine-orm symfony-forms entitymanager


    【解决方案1】:

    如果您使用 EntityType 字段类,entry_type 不是预期的类型。它希望使用来自yourbundleapp/Entity 的通过 Doctrine 绑定到您的数据库的实体。 EntityType 的行为类似于 ChoiceType,但它直接与参数类中声明的 Doctrine 实体交互。你可以在这里找到它的工作原理:https://symfony.com/doc/current/reference/forms/types/entity.html

    据我了解,您希望能够在您的应用程序上下载文件,因此您可能难以理解提交表单在 Symfony 上的工作方式。

    您必须首先定义您的新实体(来自 yourbundleapp/Entity),然后将其作为参数传递给您的表单(来自 yourbundleapp/Form),如下所示:

    $image = new ShopProductImages(); $form = $this->get('form.factory')->create(ProductTypeNew::class, $image);

    如果需要,您还可以通过嵌入在您的第一个表单中添加表单:https://symfony.com/doc/current/form/embedded.html

    如果我理解不好,请您详细说明您想做什么以及您做了什么?

    【讨论】:

    • 感谢您的帮助,我更新了主帖,实现了嵌入的表单,但出现了新错误。
    猜你喜欢
    • 2018-09-27
    • 1970-01-01
    • 1970-01-01
    • 2019-04-19
    • 1970-01-01
    • 2021-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多