My model contains two related classes - RealEstate and Image, and for one instance of RealEstate there can be many instances of images. Since the Image class can also be used in combination with other classes, I chose the One-to-many, one-way relationship with the join table. This ensures that any image does not need to know where it is used. In turn, the RealProperty class is supplied with the $ images property, getImages (), addImage (Image $ image) and removeImage (Image $ image) methods, and $ images in the constructor is determined by an empty ArrayCollection. So, I have the following model classes.
1) App \ Entity \ RealProperty \ RealProperty
namespace App\Entity\RealProperty;
use App\Entity\Platform\Image;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
class RealProperty
{
private $id;
private $images;
public function __construct()
{
$this->images = new ArrayCollection();
}
public function getId()
{
return $this->id;
}
public function getImages()
{
return $this->images;
}
public function addImage(Image $image)
{
if (!$this->images->contains($image)) {
$this->images->add($image);
}
}
public function removeImage(Image $image)
{
$this->images->removeElement($image);
}
}
2) App\Entity\Platform\Image
namespace App\Entity\Platform;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\File;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
class Image
{
private $id;
private $imageFile;
private $imageName;
private $imageSize;
private $dateOfCreation;
private $dateOfChange;
public function __construct()
{
$currentDate = new \DateTime('NOW');
$this->dateOfCreation = $currentDate;
$this->dateOfChange = $currentDate;
}
public function getId()
{
return $this->id;
}
public function setId($id)
{
$this->id = $id;
}
public function getImageFile(): ?File
{
return $this->imageFile;
}
public function setImageFile(?File $image = null): void
{
$this->imageFile = $image;
if (null !== $image) {
$this->dateOfChange = new \DateTimeImmutable();
}
}
public function getImageName(): ?string
{
return $this->imageName;
}
public function setImageName(?string $imageName)
{
$this->imageName = $imageName;
}
public function getImageSize(): ?int
{
return $this->imageSize;
}
public function setImageSize(?int $imageSize)
{
$this->imageSize = $imageSize;
}
public function getDateOfCreation(): ?\DateTime
{
return $this->dateOfCreation;
}
public function setDateOfCreation(?\DateTime $dateOfCreation)
{
$this->dateOfCreation = $dateOfCreation;
}
public function getDateOfChange(): ?\DateTime
{
return $this->dateOfChange;
}
public function setDateOfChange(?\DateTime $dateOfChange)
{
$this->dateOfChange = $dateOfChange;
}
}
.
1) App\Form\RealProperty\RealPropertyType
namespace App\Form\RealProperty;
use App\Entity\RealProperty\RealProperty;
use App\Form\Platform\ImageType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class RealPropertyType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('images', CollectionType::class, array(
'entry_type' => ImageType::class,
'label' => false,
'allow_add' => true,
'allow_delete' => true,
'prototype' => true,
'by_reference' => false
))
->add('submit', SubmitType::class, [
'label' => '',
'attr' => [
'class' => 'btn btn-sm btn-primary col-6 mx-auto',
'style' => 'display: block;'
]
])
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'App\Entity\RealProperty\RealProperty'
));
}
public function getBlockPrefix()
{
return 'real_property_real_property';
}
}
2) \\\ImageType
<?php
namespace App\Form\Platform;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Vich\UploaderBundle\Form\Type\VichImageType;
class ImageType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('imageFile', VichImageType::class, array(
'label' => false,
'required' => true
))
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'App\Entity\Platform\Image'
));
}
public function getBlockPrefix()
{
return 'platform_image';
}
}
, , CollectionType.
<?php
namespace App\Controller\RealProperty;
use App\Entity\RealProperty\RealProperty;
use App\Form\RealProperty\RealPropertyType;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class RealPropertyController extends Controller
{
public function newAction(Request $request) {
$realProperty = new RealProperty();
$form = $this->createForm(RealPropertyType::class, $realProperty);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($realProperty);
$em->flush();
return $this->redirectToRoute('real_property_index');
}
return $this->render('RealProperty/RealProperty/new.html.twig', [
'realProperty' => $realProperty,
'form' => $form->createView(),
]);
}
}
ArrayCollection, , , CollectionType .
, Vich/UploaderBundle, , ... - ! . ImageController, newAction() ImageType, .
, - ArrayCollection "--, ". .
, , . . - git.