【发布时间】:2019-11-11 14:04:47
【问题描述】:
参见上面的编辑
我认为这个问题很容易解决,但我现在找不到任何明确的答案。我希望你能有一个想法。
我正在尝试使用奏鸣曲管理员上传图片。
在我的实体中我有这个字段
/**
* @ORM\Column(type="string", length=2000)
* @Assert\File(mimeTypes={ "image/png", "image/jpeg" })
*/
private $image;
当我进入奏鸣曲管理表单视图时。按钮上传文件在那里,定义如下
$formMapper->add('image', FileType::class);
但是当我尝试发送表单时,我收到了这个错误
表单的视图数据应该是 Symfony\Component\HttpFoundation\File\File 类的实例,但它是一个 (n) 字符串。您可以通过将“data_class”选项设置为 null 或添加将 a(n) 字符串转换为 Symfony\Component\HttpFoundation\File\File 实例的视图转换器来避免此错误。
我暗示这是由于教义字符串类型。但我不认为教义有“文件”类型。
感谢您的帮助。
编辑:
考虑到评论中提供的链接,这是新的错误
当前字段
image未链接到管理员。请为目标实体创建一个:``
<?php
namespace App\Entity;
// src/Entity/Image.php
class Image{
const SERVER_PATH_TO_IMAGE_FOLDER = '/public/images';
/**
* Unmapped property to handle file uploads
*/
private $file;
/**
* @param UploadedFile $file
*/
public function setFile(UploadedFile $file = null)
{
$this->file = $file;
}
/**
* @return UploadedFile
*/
public function getFile()
{
return $this->file;
}
/**
* Manages the copying of the file to the relevant place on the server
*/
public function upload()
{
// the file property can be empty if the field is not required
if (null === $this->getFile()) {
return;
}
// we use the original file name here but you should
// sanitize it at least to avoid any security issues
// move takes the target directory and target filename as params
$this->getFile()->move(
self::SERVER_PATH_TO_IMAGE_FOLDER,
$this->getFile()->getClientOriginalName()
);
// set the path property to the filename where you've saved the file
$this->filename = $this->getFile()->getClientOriginalName();
// clean up the file property as you won't need it anymore
$this->setFile(null);
}
/**
* Lifecycle callback to upload the file to the server.
*/
public function lifecycleFileUpload()
{
$this->upload();
}
/**
* Updates the hash value to force the preUpdate and postUpdate events to fire.
*/
public function refreshUpdated()
{
$this->setUpdated(new \DateTime());
}
// ... the rest of your class lives under here, including the generated fields
// such as filename and updated
}
在我的 ForumAdmin 中,现在我有
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper->add('name', TextType::class);
$formMapper->add('description', TextAreaType::class);
$formMapper->add('weight', IntegerType::class);
$formMapper->add('category', EntityType::class, [
'class' => Category::class,
'choice_label' => 'name',
]);
$formMapper
->add('image', AdminType::class)
;
}
protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
$datagridMapper->add('name');
$datagridMapper->add('category');
}
protected function configureListFields(ListMapper $listMapper)
{
$listMapper->addIdentifier('name');
$listMapper->addIdentifier('description');
$listMapper->addIdentifier('category');
$listMapper->addIdentifier('weight');
$listMapper->addIdentifier('createdAt');
$listMapper->addIdentifier('updatedAt');
$listMapper->addIdentifier('image');
}
public function prePersist($object)
{
parent::prePersist($object);
$this->manageEmbeddedImageAdmins($object);
if($object instanceof Forum){
$object->setCreatedAt(new \DateTime('now'));
$object->setUpdatedAt(new \DateTime('now'));
$object->setStatus("NO_NEW");
}
}
public function preUpdate($page)
{
$this->manageEmbeddedImageAdmins($page);
}
private function manageEmbeddedImageAdmins($page)
{
// Cycle through each field
foreach ($this->getFormFieldDescriptions() as $fieldName => $fieldDescription) {
// detect embedded Admins that manage Images
if ($fieldDescription->getType() === 'sonata_type_admin' &&
($associationMapping = $fieldDescription->getAssociationMapping()) &&
$associationMapping['targetEntity'] === 'App\Entity\Image'
) {
$getter = 'get'.$fieldName;
$setter = 'set'.$fieldName;
/** @var Image $image */
$image = $page->$getter();
if ($image) {
if ($image->getFile()) {
// update the Image to trigger file management
$image->refreshUpdated();
} elseif (!$image->getFile() && !$image->getFilename()) {
// prevent Sf/Sonata trying to create and persist an empty Image
$page->$setter(null);
}
}
}
}
}
我也有这个 ImageAdmin,即使我不明白它为什么有用
final class ImageAdmin extends AbstractAdmin{
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->add('image', FileType::class);
}
public function prePersist($image)
{
$this->manageFileUpload($image);
}
public function preUpdate($image)
{
$this->manageFileUpload($image);
}
private function manageFileUpload($image)
{
if ($image->getFile()) {
$image->refreshUpdated();
}
}
// ...
}
【问题讨论】:
-
已经有这方面的食谱条目,您是否遵循该建议? sonata-project.org/bundles/admin/3-x/doc/cookbook/…
-
我没有。但我已经看到了。我告诉自己,为此创建一个 Image 类可能有点矫枉过正,它可能存在其他解决方案。那时可能没有。
-
@AlexanderKludt 我一直在尝试从中实现解决方案,但看起来我仍然没有它。
标签: symfony doctrine-orm symfony4 sonata-admin