如何仅使用一个文件小部件上传多个图像?目前我已经禁用了图像的映射(否则它会尝试映射到对象的值,但是它会在视图中抱怨,因为它是一个数组),但我不知道如何让它们重新进入服务。图像数组。什么是最好的解决方案?
服务类
/**
* Service
*
* @ORM\Table(name="services")
* @ORM\Entity
* @Gedmo\TranslationEntity(class="xxx\SiteBundle\Entity\ServiceTranslation")
*/
class Service
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @Gedmo\Translatable
* @ORM\Column(name="name", type="string", length=255)
*/
private $name;
/**
* @var string
*
* @ORM\Column(name="price", type="decimal", scale=2)
*/
private $price;
/**
* @var string
*
* @Gedmo\Slug(fields={"name"})
* @ORM\Column(name="slug", type="string", length=128)
*/
private $slug;
/**
* @var string
*
* @Gedmo\Translatable
* @ORM\Column(name="description", type="text")
*/
private $description;
/**
* @ORM\OneToMany(
* targetEntity="xxx\SiteBundle\Entity\ServiceTranslation",
* mappedBy="object",
* cascade={"persist", "remove"}
* )
*/
private $translations;
/**
* @ORM\OneToMany(
* targetEntity="xxx\SiteBundle\Entity\Image", mappedBy="service",
* cascade={"persist", "remove"}
* )
*/
private $images;
public function __construct()
{
$this->translations = new ArrayCollection();
$this->images = new ArrayCollection();
}
// everything else
}
图片类:
/**
* Image class.
*
* @ORM\Table(name="images")
* @ORM\Entity
* @Gedmo\Uploadable(filenameGenerator="SHA1", allowOverwrite=true, appendNumber=true)
*/
class Image
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
* @Gedmo\UploadableFileName
*/
private $name;
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @Gedmo\UploadableFilePath
*/
private $path;
/**
* @var
*/
private $file;
/**
* @ORM\ManyToOne(targetEntity="xxx\SiteBundle\Entity\Service", inversedBy="images")
* @ORM\JoinColumn(name="service_id", referencedColumnName="id")
*
*/
private $service;
// everything else
}
服务表单类型:
$builder
->add('translations', 'a2lix_translations_gedmo', array(
'translatable_class' => 'xxx\SiteBundle\Entity\Service',
'label' => false,
'fields' => array(
'name' => array(),
'price' => array(
'locale_options' => array(
'lt' => array(
'display' => false,
),
),
),
'description' => array(),
),
))
->add('images', 'file', array(
'data_class' => 'xxx\SiteBundle\Entity\Image',
'required' => false,
'attr' => array(
'accept' => 'image/*',
'multiple' => 'multiple',
),
'mapped' => false,
))
;
控制器
if ($form->isValid()) {
$uploadableManager = $this->get('stof_doctrine_extensions.uploadable.manager');
foreach ($service->getImages() as $image) {
$uploadableManager->markEntityToUpload(
$image,
$image->getFile()
);
}
$em = $this->getDoctrine()->getManager();
$em->persist($service);
$em->flush();
答案 0 :(得分:1)
好吧,好像我让它运转了。
$files = $request->files->get($form->getName());
foreach ($files['images'] as $imageData) {
$image = new Image();
$uploadableManager->markEntityToUpload(
$image,
$imageData
);
$em->persist($image);
}
是的,它看起来不太好,但有效..
答案 1 :(得分:0)
另一个细节是为了节省其他时间:你应该在文件字段的选项中将属性 multiple 设置为布尔值,而不是手动将其设置为'attr':
->add('images', 'file', array(
'data_class' => 'xxx\SiteBundle\Entity\Image',
'required' => false,
'attr' => array(
'accept' => 'image/*'
),
'multiple' => true,
'mapped' => false,
))
此更改确保 $ files ['images'] 在任何情况下都在数组中,因为当我只选择1个图像时,返回的对象没有数组,我无法迭代它与 foreach 。