I have the following error
My two classes :
<?php
//Entity/Cars.php
namespace App\Entity;
use App\Repository\CarsRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
#[ORM\Entity(repositoryClass: CarsRepository::class)]
#[Vich\Uploadable]
class Cars
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column(length: 255)]
private ?string $Brand = null;
#[ORM\Column(length: 255)]
private ?string $Model = null;
#[ORM\Column]
private ?int $Year = null;
#[ORM\Column]
private ?int $Kilometers = null;
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $Description = null;
#[ORM\Column(length: 255)]
private ?string $TypeFuel = null;
#[Vich\UploadableField(mapping: "cars", fileNameProperty: "image", mimeType: "image/jpeg")]
#[ORM\OneToMany(mappedBy: 'cars', targetEntity: Images::class, orphanRemoval: true)]
private ?Images $Image = null;
public function __construct()
{
//$this->Image = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getBrand(): ?string
{
return $this->Brand;
}
public function setBrand(string $Brand): static
{
$this->Brand = $Brand;
return $this;
}
public function getModel(): ?string
{
return $this->Model;
}
public function setModel(string $Model): static
{
$this->Model = $Model;
return $this;
}
public function getYear(): ?int
{
return $this->Year;
}
public function setYear(int $Year): static
{
$this->Year = $Year;
return $this;
}
public function getKilometers(): ?int
{
return $this->Kilometers;
}
public function setKilometers(int $Kilometers): static
{
$this->Kilometers = $Kilometers;
return $this;
}
public function getDescription(): ?string
{
return $this->Description;
}
public function setDescription(?string $Description): static
{
$this->Description = $Description;
return $this;
}
public function getTypeFuel(): ?string
{
return $this->TypeFuel;
}
public function setTypeFuel(string $TypeFuel): static
{
$this->TypeFuel = $TypeFuel;
return $this;
}
/**
* @return Collection<int, Images>
*/
public function getImage(): ?Images
{
return $this->Image;
}
public function addImage(Images $image): self
{
/*if (!$this->Image->getCars($image)) {
$this->Image->addImage($image);
$image->setCars($this);
}*/
$this->Images = $image;
return $this;
}
public function removeImage(Images $image): static
{
if ($this->Image->removeElement($image)) {
// set the owning side to null (unless already changed)
if ($image->getCars() === $this) {
$image->setCars(null);
}
}
return $this;
}
}
<?php
//Entity/Images.php
namespace App\Entity;
use App\Repository\ImagesRepository;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
#[UniqueEntity('Name')]
#[ORM\Entity(repositoryClass: ImagesRepository::class)]
#[Vich\Uploadable]
class Images
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column(length: 255)]
private ?string $Name = null;
#[ORM\Column(length: 255)]
private ?string $Path = null;
#[ORM\ManyToOne(inversedBy: 'Image')]
#[ORM\JoinColumn(nullable: false)]
private ?Cars $cars = null;
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->Name;
}
public function setName(string $Name): static
{
$this->Name = $Name;
return $this;
}
public function getPath(): ?string
{
return $this->Path;
}
public function setPath(string $Path): static
{
$this->Path = $Path;
return $this;
}
public function getCars(): ?Cars
{
return $this->cars;
}
public function setCars(?Cars $cars): self
{
// unset the owning side of the relation if necessary
if ($cars === null && $this->cars !== null) {
$this->cars->addImage(null);
}
// set the owning side of the relation if necessary
if ($cars !== null && $cars->getImage() !== $this) {
$cars->addImage($this);
}
$this->cars = $cars;
//$this->cars = $cars;
return $this;
}
}
My form
<?php
namespace App\Form;
use App\Entity\Cars;
use App\Entity\Images;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\Extension\Core\Type\NumberType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\HttpKernel\Controller\ArgumentResolver\BackedEnumValueResolver;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Vich\UploaderBundle\Form\Type\VichFileType;
use Vich\UploaderBundle\Form\Type\VichImageType;
class CarsType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$group = new Cars();
$builder
->add('Brand')
->add('Model')
->add('Year')
->add('Kilometers')
->add('Description')
->add('TypeFuel')
->add('Image',EntityType::class,[
'class'=> Images::class,
'choices'=>$group->getId(),
])
/*->add('Image', VichImageType::class, [
'delete_label' => 'Remove file',
]*/
//->add("Image", CollectionType::class)
/*->add('Image', VichFileType::class, [
'entry_type' => CollectionType::class,
'entry_options' => [
'label' => false,
'required' => false,
'allow_delete' => true,
'download_uri' => true,
// Ajoutez d'autres options selon vos besoins
],
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
'label' => 'Images', // Étiquette pour le champ de collection
])*/;
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => Cars::class,
]);
}
}
How to type this to have a dialog box and make a relation to the picture in Class Entity ? Trying to find solution for this 'generic' problem and cannot find the right one. Trying to adapt the solution of doc https://symfony.com/doc/current/reference/forms/types/entity.html
Trying to find solution in google search... https://symfony.com/doc/current/reference/forms/types/entity.html