Я пытаюсь заставить этот пакет работать с формой.
Сначала это моя сущность
<?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\Entity\File as EmbeddedFile;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
/**
* @ORM\Entity(repositoryClass="App\Repository\BanqueRepository")
* @Vich\Uploadable
*/
class Banque
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=50)
*/
private $libelle;
/**
* NOTE: This is not a mapped field of entity metadata, just a simple property.
*
* @Vich\UploadableField(mapping="banques_image", fileNameProperty="image.name", size="image.size", mimeType="image.mimeType", originalName="image.originalName", dimensions="image.dimensions")
*
* @var File
*/
private $imageFile;
/**
* @ORM\Column(type="datetime")
*
* @var \DateTime
*/
private $updatedAt;
/**
* @ORM\Embedded(class="Vich\UploaderBundle\Entity\File")
*
* @var EmbeddedFile
*/
private $image;
public function getId(): ?int
{
return $this->id;
}
public function getLibelle(): ?string
{
return $this->libelle;
}
public function setLibelle(string $libelle): self
{
$this->libelle = $libelle;
return $this;
}
/**
* If manually uploading a file (i.e. not using Symfony Form) ensure an instance
* of 'UploadedFile' is injected into this setter to trigger the update. If this
* bundle's configuration parameter 'inject_on_load' is set to 'true' this setter
* must be able to accept an instance of 'File' as the bundle will inject one here
* during Doctrine hydration.
*
* @param File|UploadedFile $image
*/
public function setImageFile(?File $image = null)
{
$this->imageFile = $image;
if (null !== $image) {
// It is required that at least one field changes if you are using doctrine
// otherwise the event listeners won't be called and the file is lost
$this->updatedAt = new \DateTimeImmutable();
}
}
public function getImageFile(): ?File
{
return $this->imageFile;
}
public function setImage(EmbeddedFile $image)
{
$this->image = $image;
}
public function getImage(): ?EmbeddedFile
{
return $this->image;
}
}
и «Форма» для этой сущности:
<?php
namespace App\Form;
use App\Entity\Banque;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Vich\UploaderBundle\Form\Type\VichImageType;
class BanqueType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('libelle', TextType::class)
->add('imageFile', VichImageType::class)
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => Banque::class,
));
}
}
а теперь самый простой контроллер;)
<?php
namespace App\Controller;
use App\Entity\Banque;
use App\Form\BanqueType;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
class BanqueController extends AbstractController
{
/**
* @Route("/banque/add", name="banque_add")
*/
public function add(Request $request)
{
$banque = new Banque();
$form = $this->createForm(BanqueType::class, $banque, array('action' => $this->generateUrl('banque_add')));
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$banque = $form->getData();
$em = $this->getDoctrine()->getManager();
$em->persist($banque);
$em->flush();
return $this->redirectToRoute('index');
}
return $this->render(
'banque/form.html.twig',
array('form' => $form->createView(),
'icone'=> 'mdi-plus',
'text' => 'Création'
)
);
}
}
Но когда я перехожу к "/ banque / add", у меня возникает исключение:
PropertyAccessor для работы требуется граф объектов или массивов, но он нашел тип "NULL" при попытке пройти путь "image.name" в свойстве "name".
Я знаю, что что-то не так, но я не нашел, что это такое.Любая помощь будет признательна!
спасибо всем,