我正在尝试在Sonata Admin中构建CRUD功能,并希望在Product Admin(而非Image Admin)上上传图像。但是我收到此错误,并且不知道为什么单击“编辑”时会出现
未找到字段“图像”的映射
我是Symfony的新手。我在Image Admin上成功上传了图像,但我不知道如何在Product Admin上上传并同时上传许多图像。这是我的代码:
一个产品有很多图像,因此以下是我的产品,图像实体和产品管理员
Product.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;
}
}
Image.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,
])
;
}