Multi upload files on symfony2.8

2019-09-06 03:41发布

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"

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

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

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

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 it and thank you.

1条回答
Animai°情兽
2楼-- · 2019-09-06 04:07

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

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

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

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

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()));
    }
查看更多
登录 后发表回答