[更新]:我解决了我的问题> <,也许在完成重构后,我的方法getFile上留下了“:?string” ...
今天,我尝试在项目中添加VichUploderBundle,但是遇到了一些麻烦。 我已经按照文档进行操作,但是我不喜欢使一个属性可上载的方法(为此,请在同一模型上添加3个属性,对不起,但是还不够漂亮) 因此,我开始为Image创建一个具有所有必要属性的实体,我还创建了一个新的FormType并将所有链接到我的父实体。 但是现在当我尝试发布表单时,出现此错误:
Return value of Vich\UploaderBundle\Mapping\PropertyMapping::getFile() must be an instance of Symfony\Component\HttpFoundation\File\File or null, string returned
(in vendor/vich/uploader-bundle/Mapping/PropertyMapping.php (line 80) )
我试图添加一些转储来检查变量,但变量存在,但出于奇怪的原因,Vich将其检测为空。
图片实体
<?php
namespace App\Entity;
use App\Entity\Traits\CreatedAtTrait;
use App\Entity\Traits\CreatedByTrait;
use Doctrine\ORM\Mapping as ORM;
use Exception;
use Symfony\Component\HttpFoundation\File\File;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
/**
* @ORM\Entity(repositoryClass="App\Repository\ImageRepository")
* @Vich\Uploadable
*/
class Image
{
use CreatedAtTrait;
use CreatedByTrait;
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @Vich\UploadableField(mapping="compose_logo", fileNameProperty="name", size="size")
* @var File
*/
private $file;
/**
* @ORM\Column(type="string", length=255)
*/
private $name;
/**
* @ORM\Column(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;
}
}
ImageType
<?php
namespace App\Form;
use App\Entity\Image;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Vich\UploaderBundle\Form\Type\VichImageType;
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 App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="App\Repository\ParentRepository")
*/
class Parent
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $name;
/**
* @ORM\OneToOne(targetEntity="App\Entity\Image", 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;
}
}
ParentType
<?php
namespace App\Form;
use App\Entity\Parent;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
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尝试将实体保存到数据库中时,除我的createdAt特性外,我的Image实体中的所有属性为null (但那可能很正常,因为在那之前我有例外...)
当我在vendor / vich / uploader-bundle / Mapping / PropertyMapping.php的第80行转储变量时,也有一个屏幕截图: https://imgur.com/NCEft1s
答案 0 :(得分:0)
您需要在ParentType类中将“徽标”属性定义为 EntityType 而不是ImageType!
因此,更改此行->add('logo', ImageType::class)
您在symfony文档中有示例:https://symfony.com/doc/current/reference/forms/types/entity.html