我需要开发一个具有很多图片的实体 Annonce 这是我的尝试,但不起作用:/
匿名实体:
<?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\AnnonceRepository")
*
*/
class Annonce
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Image", mappedBy="annonce", orphanRemoval=true)
*/
private $images;
public function __construct()
{
$this->images = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
/**
* @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->setAnnonce($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->getAnnonce() === $this) {
$image->setAnnonce(null);
}
}
return $this;
}
}
图像实体:
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\File;
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)
*/
private $name;
/**
* @ORM\Column(type="string", length=255)
*/
private $image;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Annonce", inversedBy="images")
* @ORM\JoinColumn(nullable=false)
*/
private $annonce;
/**
* @var File
*
* @Vich\UploadableField(mapping="images", fileNameProperty="image")
*/
private $imageFile;
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 getImage(): ?string
{
return $this->image;
}
public function setImage(string $image): self
{
$this->image = $image;
return $this;
}
public function getAnnonce(): ?Annonce
{
return $this->annonce;
}
public function setAnnonce(?Annonce $annonce): self
{
$this->annonce = $annonce;
return $this;
}
/**
* @param File|null $image
* @return Image
* @throws \Exception
*/
public function setImageFile(File $image = null)
{
$this->imageFile = $image;
if ($image) {
$this->updatedAt = new \DateTime('now');
}
return $this;
}
/**
* @return File
*/
public function getImageFile()
{
return $this->imageFile;
}
public function __toString()
{
return (string)$this->image;
}
}
这是ImageType:
<?php
/**
* Created by PhpStorm.
* User: Trigui
* Date: 12/03/2019
* Time: 10:35
*/
namespace App\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Vich\UploaderBundle\Form\Type\VichFileType;
class ImageType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('imageFile', VichFileType::class)
;
}
/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'App\Entity\Image'
));
}
}
最终结局类型
<?php
namespace App\Form;
use App\Entity\Annonce;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Vich\UploaderBundle\Form\Type\VichImageType;
class AnnonceType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('images', CollectionType::class, array(
'entry_type' => ImageType::class,
'prototype' => true,
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
'required' => false,
'label' => false,
));
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Annonce::class,
]);
}
}
Ps:Annonce-> Images的关系是一对多
我看不到输入“ FILE”的问题,因此我无法添加图像。 我已经尝试了很多,但无法解决
我已经在这里上传了TestProject: https://github.com/khaliltr/multipleFiles
答案 0 :(得分:1)
查看文档https://symfony.com/doc/current/reference/forms/types/collection.html#basic-usage
“除非您的
Annonce
包含一些输入字段,否则不会显示任何输入字段images
”
因此,您需要手动添加一个空的image
。在您的createAction
和editAction
中这样:
if ($annonce->getImages()->isEmpty()) {
$image = new Image();
$image->setAnnonce($annonce);
$annonce->getImages()->add($image);
}