本文介绍了symfony2.8 上的多上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 symfony2.8 上做了一个多上传文件,我发现总是有问题,我总是得到这个:类型为字符串"的预期参数,给定的数组"

i was doing a multi upload files on symfony2.8 and i find always problems,and i always got this :"Expected argument of type "string", "array" given"

这是我的实体/Article.php

this is my entity /Article.php

<?php

namespace RoubBundle\Entity;

use Symfony\Component\HttpFoundation\File\File;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\HttpFoundation\File\UploadedFile;


/**
 * Article
 *
 * @ORM\Table(name="article")
 * @ORM\Entity(repositoryClass="RoubBundle\Repository\ArticleRepository")
 */
class Article
{
/**
 * @var int
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @ORM\Column(type="string", nullable=true)
 *
 * @Assert\File(
 *      maxSize="5242880",
 *      mimeTypes = {
 *          "image/png",
 *          "image/jpeg",
 *          "image/jpg",
 *          "image/gif"
 *      }
 * )
 */
public $image= array();

/**
 * @ORM\Column(type="string")
 * @var string
 */
private $titre;

public function getTitre()
{
    return $this->titre;
}

public function setTitre($titre)
{
    $this->titre = $titre;

    return $this;
}


public function getImage() {
    return $this->image;
}
public function setImage(array $image) {
    $this->image = $image;
}

/**
 * Get id
 *
 * @return integer
 */
public function getId()
{
    return $this->id;
}
}

这是我在控制器/ArticleController.php 中的操作 NewAction

and this is my action NewAction in my controller /ArticleController.php

public function newAction(Request $request)
{
    $article = new Article();
    $form = $this->createForm('RoubBundle\Form\ArticleType', $article);
    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
    if (null !== $article->getImage) {
    foreach($file as $article->getImage) {

/** @var Symfony\Component\HttpFoundation\File\UploadedFile $file */
        $file = $article->getImage();

        $fileName = md5(uniqid()).'.'.$file->guessExtension();

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

        array_push($article->getImage(), $fileName);
        }
        }
        $em = $this->getDoctrine()->getManager();
        $em->persist($article);
        $em->flush();

        return $this->redirectToRoute('article_show', array('id' => $article->getId()));
    }

    return $this->render('RoubBundle:article:new.html.twig', array(
        'article' => $article,
        'form' => $form->createView(),
    ));
}

这是表单/ArticleType.php

this is form /ArticleType.php

    public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('titre')
        ->add('image', 'file', array(
                    'required' => false,
                        'data_class' => null,
                    "multiple" => "multiple"))
    ;
}

我在我的树枝/new.html.twig 中尝试这个

and i try this in my twig /new.html.twig

    {{ form_start(form) }}
 {{ form_widget(form.titre) }} </br>
 {{ form_widget(form.image, { 'attr': { 'multiple': 'multiple' } }) }}
                             </br>
    <input type="submit" value="Create" />
{{ form_end(form) }}

伙计们,我希望有人能帮助我,我真的很紧张谢谢.

guys, i wish that someone could help me, i am really stressed about itand thank you.

推荐答案

您的问题是实体中的 $image 是字符串类型:

Your problem is that $image is of type string in your entity:

@ORM\Column(type="string", nullable=true)

因此,您首先需要弄清楚如何在数据库中保存图像列表.这样做的好方法是创建一个名为 article_images 的新表(和实体),例如如下所示:

So you need first to figure out how you want to save list of images in your database. The good way to do this would be creating a new table (and Entity) called article_images which looks like this for example:

$id
$imageUrl
$articleId

如果您不想要另一个表,您可以尝试将图像数组保存为 json.要做到这一点,请使用

If you dont want another table you can try and save your images array as a json. To do this use

 @ORM\Column(type="json")

在您的 $image 字段上.

on your $image field.

第二个问题是您的移动"代码.您在数组末尾推送值,而您只想要移动"的值.下面是它的样子:

Second problem is your "moving" code. You are pushing values at the end of array, while you only want the "moved" ones. Here is what it could look like:

 if ($article->getImage()) {

    $movedImages = array();

    foreach($article->getImage() as $index=>$file) {

        $fileName = md5(uniqid()).'.'.$file->guessExtension();

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

        array_push($movedImages, $fileName);
        }

        $article->setImage($movedImages);
        }
        $em = $this->getDoctrine()->getManager();
        $em->persist($article);
        $em->flush();

        return $this->redirectToRoute('article_show', array('id' => $article->getId()));
    }

这篇关于symfony2.8 上的多上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 14:31