VichUploaderBundle & OneToOne Relation



[更新]:我>

今天我试图在我的项目中添加VichUploderBundle,但我遇到了一些麻烦。 我已经遵循了文档,但我不喜欢使一个属性可上传的方式(为此在同一模型上添加 3 个属性,对不起,但那不是很漂亮) 所以我开始为图像创建一个具有所有必要属性的实体,我还创建了一个新的 FormType 并将所有实体链接到我的父实体。 但是现在当我尝试发布我的表格时,我收到此错误:

Return value of VichUploaderBundleMappingPropertyMapping::getFile() must be an instance of SymfonyComponentHttpFoundationFileFile or null, string returned
(in vendor/vich/uploader-bundle/Mapping/PropertyMapping.php (line 80) )

我试图添加一些转储来检查变量,她存在,但由于一个奇怪的原因,Vich 将其检测为空。

图像实体

<?php
namespace AppEntity;
use AppEntityTraitsCreatedAtTrait;
use AppEntityTraitsCreatedByTrait;
use DoctrineORMMapping as ORM;
use Exception;
use SymfonyComponentHttpFoundationFileFile;
use VichUploaderBundleMappingAnnotation as Vich;
/**
* @ORMEntity(repositoryClass="AppRepositoryImageRepository")
* @VichUploadable
*/
class Image
{
use CreatedAtTrait;
use CreatedByTrait;
/**
* @ORMId()
* @ORMGeneratedValue()
* @ORMColumn(type="integer")
*/
private $id;
/**
* @VichUploadableField(mapping="compose_logo", fileNameProperty="name", size="size")
* @var File
*/
private $file;
/**
* @ORMColumn(type="string", length=255)
*/
private $name;
/**
* @ORMColumn(type="integer")
*/
private $size;
public function getId(): ?int
{
return $this->id;
}
public function getFile(): ?string
{
return $this->file;
}
/**
* If manually uploading a file (i.e. not using Symfony Form) ensure an instance
* of 'UploadedFile' is injected into this setter to trigger the update. If this
* bundle's configuration parameter 'inject_on_load' is set to 'true' this setter
* must be able to accept an instance of 'File' as the bundle will inject one here
* during Doctrine hydration.
*
* @param File|null $file
* @return Image
* @throws Exception
*/
public function setFile(?File $file = null): self
{
$this->file = $file;
return $this;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getSize(): ?int
{
return $this->size;
}
public function setSize(int $size): self
{
$this->size = $size;
return $this;
}
}

图像类型

<?php
namespace AppForm;
use AppEntityImage;
use SymfonyComponentFormAbstractType;
use SymfonyComponentFormFormBuilderInterface;
use SymfonyComponentOptionsResolverOptionsResolver;
use VichUploaderBundleFormTypeVichImageType;
class ImageType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('file', VichImageType::class, [
'required' => false,
])
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Image::class,
]);
}
}

父实体

<?php
namespace AppEntity;
use DoctrineCommonCollectionsArrayCollection;
use DoctrineCommonCollectionsCollection;
use DoctrineORMMapping as ORM;
/**
* @ORMEntity(repositoryClass="AppRepositoryParentRepository")
*/
class Parent
{
/**
* @ORMId()
* @ORMGeneratedValue()
* @ORMColumn(type="integer")
*/
private $id;
/**
* @ORMColumn(type="string", length=255)
*/
private $name;
/**
* @ORMOneToOne(targetEntity="AppEntityImage", cascade={"persist", "remove"})
*/
private $logo;
public function __construct()
{
$this->variables = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}

public function getLogo(): ?Image
{
return $this->logo;
}
public function setLogo(?Image $logo): self
{
$this->logo = $logo;
return $this;
}
}

父类型

<?php
namespace AppForm;
use AppEntityParent;
use SymfonyComponentFormAbstractType;
use SymfonyComponentFormExtensionCoreTypeCollectionType;
use SymfonyComponentFormExtensionCoreTypeTextareaType;
use SymfonyComponentFormExtensionCoreTypeTextType;
use SymfonyComponentFormFormBuilderInterface;
use SymfonyComponentOptionsResolverOptionsResolver;
class ParentType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('logo', ImageType::class)
->add('name', TextType::class)
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Parent::class,
]);
}
}

对于测试,我还尝试删除vendor/vich/uploader-bundle/Mapping/PropertyMapping中的注释.php以接受所有变量类型 当 Doctrine 尝试将实体保存在数据库中时,我的图像实体中的所有属性都是空的,除了我创建的 At 特征 (但这可能是正常的,因为我在此之前有例外......

当我在vendor/vich/uploader-bundle/Mapping/PropertyMapping中的第80行转储变量时,还有一个屏幕截图.php : https://i.stack.imgur.com/MJh2g.jpg

您需要将"logo"属性定义为ParentType类中的EntityType,而不是ImageType! 所以把这行改->add('logo', ImageType::class)

你在symfony文档中有例子:https://symfony.com/doc/current/reference/forms/types/entity.html

相关内容

  • 没有找到相关文章