image - 在 Sonata Admin Symfony 4.2 中上传多个图像
问题描述
我正在尝试在 Sonata Admin 中构建一个 CRUD 功能,并希望在 Product Admin(而不是 Image Admin)上传图像。但是当我单击编辑时出现此错误并且不知道为什么:
找不到字段“图像”的映射
我是 Symfony 的新手。我在 Image Admin 成功上传了图片,但我不知道如何在 Product Admin 上传并同时上传许多图片。这是我的代码:
一个产品有很多图片,所以下面是我的产品、图片实体和产品管理员
产品.php
<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
/**
* @ORM\Entity(repositoryClass="App\Repository\ProductRepository")
* @Vich\Uploadable
*/
class Product
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $name;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Image", mappedBy="product")
*/
private $images;
public function __construct()
{
$this->images = new ArrayCollection();
}
public function __toString()
{
return (string) $this->getName();
}
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;
}
/**
* @return Collection|Image[]
*/
public function getImages(): Collection
{
return $this->images;
}
public function addImage(Image $image): self
{
if (!$this->images->contains($image)) {
$this->images[] = $image;
$image->setProduct($this);
}
return $this;
}
public function removeImage(Image $image): self
{
if ($this->images->contains($image)) {
$this->images->removeElement($image);
// set the owning side to null (unless already changed)
if ($image->getProduct() === $this) {
$image->setProduct(null);
}
}
return $this;
}
}
图片.php
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
/**
* @ORM\Entity(repositoryClass="App\Repository\ImageRepository")
* @Vich\Uploadable
*/
class Image
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
* @var string $name
*/
private $name;
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @var string $path
*/
private $path;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Product", inversedBy="images")
*/
private $product;
/**
* @Vich\UploadableField(mapping="product_images", fileNameProperty="path")
* @var File $imageFile
*/
private $imageFile;
/**
* @ORM\Column(type="datetime")
* @var \DateTime $updatedAt
*/
private $updatedAt;
/**
* @return File
*/
public function getImageFile()
{
return $this->imageFile;
}
/**
* @param File|\Symfony\Component\HttpFoundation\File\UploadedFile
*/
public function setImageFile(File $path = null) : void
{
$this->imageFile = $path;
if($path){
$this->updatedAt = new \DateTime('now');
}
}
public function __toString()
{
return (string) $this->getName();
}
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 getPath(): ?string
{
return $this->path;
}
public function setPath($path): self
{
$this->path = $path;
return $this;
}
public function getProduct(): ?Product
{
return $this->product;
}
public function setProduct(?Product $product): self
{
$this->product = $product;
return $this;
}
}
ProductAdmin.php
protected function configureFormFields(FormMapper $formMapper): void
{
$formMapper
->add('name')
->add('images', VichFileType::class, [
'label' => 'Image',
'mapped' => true,
])
;
}
解决方案
推荐阅读
- c# - 如何在对 Web API 的 http 发布请求中将长破折号(—)作为字符串数据的一部分发布
- java - 如何翻转列表
- 到地图
- >
- 到地图
- cmake - 在构建 add_subdirectory(B) 之前安装 add_subdirectory(A) 头文件
- c++ - 如何从 Catch2 测试用例中访问自定义命令行选项?
- pointers - 指向参数数组目标的 Fortran 指针
- swift - 如何将数组传递给 Swift 中可变参数的函数?
- python - 你如何将第一个有效值向左移动?
- python - 如何巧妙地分块读取大文件?
- android - 如何修复 CameraX 旋转支持
- mongodb - 有效使用redis和mongo进行分层列表api