Answer the question
In order to leave comments, you need to log in
Uploading multiple files in Symfony?
Hello.
I'm getting started with the Symfony framework.
The project was created on Symfony 3.0.4.
Created two entities:
<?php
namespace AppBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
/**
* Product
*
* @ORM\Table(name="product")
* @ORM\Entity(repositoryClass="AppBundle\Repository\ProductRepository")
*/
class Product
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity="Category", inversedBy="products")
* @ORM\JoinColumn(name="category_id", referencedColumnName="id")
*/
private $category;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=30)
*/
private $name;
/**
* @var string
*
* @ORM\Column(name="description", type="text")
*/
private $description;
/**
* @var string
*
* @ORM\Column(name="price", type="decimal", precision=10, scale=0)
*/
private $price;
/**
* @var \DateTime
*
* @ORM\Column(name="date", type="datetime")
*/
private $date;
/**
* @ORM\OneToMany(targetEntity="Image", mappedBy="product")
*/
private $images;
function __construct()
{
$this->images = new ArrayCollection();
}
// getters & setters ...
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Image
*
* @ORM\Table(name="image")
* @ORM\Entity(repositoryClass="AppBundle\Repository\ImageRepository")
*/
class Image
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="image", type="string", length=255)
*/
private $image;
/**
* @ORM\ManyToOne(targetEntity="Product", inversedBy="images")
* @ORM\JoinColumn(name="product_id", referencedColumnName="id")
*/
private $product;
// getters & setters ...
class ProductType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', TextType::class)
->add('description', TextareaType::class)
->add('price', NumberType::class)
->add('category', EntityType::class, array(
'class' => 'AppBundle\Entity\Category',
'choice_label' => 'category'
))
->getForm();
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'register_class' => 'AppBundle:Entity:Product'
));
}
public function getName()
{
return 'app_bundle_product_type';
}
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', TextType::class)
->add('description', TextareaType::class)
->add('price', NumberType::class)
->add('category', EntityType::class, array(
'class' => 'AppBundle\Entity\Category',
'choice_label' => 'category'
))
->add('images', ColeectionType:class) // ДОБАВЛЕННОЕ ПОЛЕ
->getForm();
}
/**
* Image
*
* @ORM\Table(name="image")
* @ORM\Entity(repositoryClass="AppBundle\Repository\ImageRepository")
*/
class Image
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=50)
*/
private $name;
/**
* @var string
*
* @ORM\Column(name="path", type="string", length=255)
*/
private $path;
/**
* @ORM\ManyToOne(targetEntity="Product", inversedBy="images")
* @ORM\JoinColumn(name="product_id", referencedColumnName="id")
*/
private $product;
/**
* @Assert\File(maxSize="6000000")
*/
private $file;
// геттеры и сеттеры
/**
* @ORM\OneToMany(targetEntity="Image", mappedBy="product", cascade={"persist"})
*/
private $images;
public function addImage(Image $image) {
$this->images[] = $image;
$image->setProduct($this);
return $this;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', TextType::class)
->add('file', FileType::class)
->getForm();
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', TextType::class)
->add('description', TextareaType::class)
->add('price', NumberType::class)
->add('category', EntityType::class, array(
'class' => 'AppBundle\Entity\Category',
'choice_label' => 'category'
))
->add('images', CollectionType::class, array(
'entry_type' => ImageType::class,
'allow_add' => true,
'by_reference' => false,
'allow_delete' => true,
'prototype' => true
))
->getForm();
}
public function addProductAction(Request $request)
{
$product = new Product();
$image = new Image();
$product->addImage($image);
$form = $this->createForm(ProductType::class, $product);
$form->handleRequest($request);
if ($form->isValid()) {
$product->setDate(new \DateTime());
$em = $this->getDoctrine()->getManager();
$em->persist($product);
$em->flush();
return $this->redirectToRoute('admin_products');
}
return $this->render('admin/add_product.html.twig', array(
'form' => $form->createView()
));
}
An exception occurred while executing 'INSERT INTO image (name, path, product_id) VALUES (?, ?, ?)' with params [null, "initial", 12]:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'name ' cannot be null
$image1 = new Image();
$product->addImage($image1);
$image2 = new Image();
$product->addImage($image2);
Answer the question
In order to leave comments, you need to log in
Made the upload without using forms.
In the template I added:
In the Action, I get an array with files and create Image objects by passing the file to setFile (file), and then I add the Image object to $product->images:
$images = $request->files->get('files');
foreach($images as $file) {
$image = new Image();
$image->setFile($file);
$product->addImage($image);
}
There are several problems in your code. Perhaps if you eliminate them, then everything will work.
1. You don't have file upload processing. It must be saved manually.
Example: https://github.com/symfony/symfony-demo/pull/286/files
In the controller, all uploaded files should be handled like this:
foreach ($product->getImages() as $image) {
$uploadedFile = $image->getFile();
$uploadedFile->move('uploads/', $uploadedFile->getClientOriginalName());
$image->setPath('uploads/' . $uploadedFile->getClientOriginalName());
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question