如何仅使用一个文件小部件上传多张图片?目前我已经禁用了图像的映射(否则它会尝试映射到对象的值,但它会在视图中抱怨,因为它是一个数组),但我不知道如何让它们重新回到服务中。图像数组。什么是最好的解决方案?
服务等级
/**
* 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();