Chào các bác, ở đây có bác nào theo Symfony cho em hỏi chút ạ.
Em đang muốn upload nhiều ảnh ở Product Admin nhưng bị lỗi này khi click Edit:
Mapping not found for field “images”
Em mới học Symfony ạ Em đã upload 1 ảnh thành công ở Image Admin nhưng giờ ko biết làm sao để upload ở Product Admin và upload nhiều ảnh 1 lúc nữa. Đây là code của em:
1 Product có nhiều Image ạ
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',
])
;
}