0

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

这是我的实体/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

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

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

我在我的树枝/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) }}

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

4

1 回答 1

1

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

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

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

$id
$imageUrl
$articleId

如果您不想要另一个表,您可以尝试将图像数组保存为 json。为此使用

 @ORM\Column(type="json")

在您的 $image 字段上。

第二个问题是您的“移动”代码。您在数组末尾推送值,而您只想要“移动”的值。这是它的样子:

 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()));
    }
于 2016-09-19T04:37:43.637 回答