我需要使用文件上传来创建表单字段,该文件也是ManyToMany实体的一部分 . 现在我的配置如下所示,它的工作原理......
class ProductTypeNew extends AbstractType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name')
->add('price')
->add('description', TextareaType::class)
->add('quantity')
->add('file', FileType::class, array('label' => 'Zdjęcie'))
;
...但我需要在控制器中手动获取表单输入并设置为表单实体
if ($form->isSubmitted() && $form->isValid())
{
$image = new ShopProductImages();
$file = $product->getFile();
$fileName = $this->generateUniqueFileName().'.'.$file->guessExtension();
$file->move(
$this->getParameter('shop_images_directory'),
$fileName
);
$image->setFile($fileName);
$product->addShopProductImages($image);
$product->setFile($fileName);
$em = $this->getDoctrine()->getManager();
$em->persist($image);
$em->persist($product);
$em->flush();
我想做这样的事情(但它不起作用):
->add('shopProductImages', EntityType::class, array(
'by_reference' => false,
'entry_type' => FileType::class,
)
带有Embeded Forms的新版本的表单类型也会导致问题:
关联字段“AppBundle \ Entity \ ShopProducts#$ shopProductImages”类型“Doctrine \ Common \ Collections \ Collection | array”的预期值,取而代之的是“Symfony \ Component \ HttpFoundation \ File \ UploadedFile” .
...具有以下配置:
ProductTypeNew:
class ProductTypeNew extends AbstractType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', null, array('label' => 'Nazwa'))
->add('price', null, array('label' => 'Cena'))
->add('description', TextareaType::class, array('label' => 'Opis'))
->add('quantity', null, array('label' => 'Ilość'))
->add('shopProductImages', ShopProductsImagesType::class);
}
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => ShopProducts::class,
]);
}
ShopProductsImagesType:
class ShopProductsImagesType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('file', FileType::class, array('label' => 'Zdjęcie'))
;
}
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
// 'data_class' => ShopProductImages::class,
'data_class' => null,
]);
}
实体店铺产品:
/**
* ShopProducts
*
* @ORM\Table(name="shop_products")
* @ORM\Entity
*/
class ShopProducts
{
....
/**
* INVERSE SIDE
*
* @var \Doctrine\Common\Collections\Collection
*
* @ORM\ManyToMany(
* targetEntity="AppBundle\Entity\ShopProductImages",
* mappedBy="shopProducts",
* cascade={"persist"}
* )
*/
private $shopProductImages;
实体ShopProductImages:
* @ORM\Entity
*/
class ShopProductImages
{
/**
* @var string
*
* @ORM\Column(name="file", type="text", length=255, nullable=true)
*/
private $file;
1 回答
如果使用EntityType字段类,则
entry_type
不是预期类型 . 它希望通过Doctrine从yourbundleapp/Entity
使用绑定到您的数据库的实体 . EntityType的作用类似于ChoiceType,但它直接与参数类中声明的Doctrine实体交互 . 你可以在这里找到它的工作原理:https://symfony.com/doc/current/reference/forms/types/entity.html根据我的理解,您希望能够在您的应用程序上下载文件,因此您可能很难理解提交表单在Symfony上的工作方式 .
您必须首先定义新实体(来自yourbundleapp / Entity),然后将其作为参数传递给您的表单(来自yourbundleapp / Form),如下所示:
$image = new ShopProductImages(); $form = $this->get('form.factory')->create(ProductTypeNew::class, $image);
如果需要,您还可以通过嵌入表单在第一个表单中添加表单:https://symfony.com/doc/current/form/embedded.html
如果我理解不好,请问您对自己想做什么以及做了什么更加啰嗦?