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.