问题
i try to make this bundle running with Form.
First this is my entity
<?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;
}
}
and the "Form" for this entity :
<?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,
));
}
}
and now the most simplest controller ;)
<?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'
)
);
}
}
But when i go to "/banque/add" i have an exception :
PropertyAccessor requires a graph of objects or arrays to operate on, but it found type "NULL" while trying to traverse path "image.name" at property "name".
I know somthing is wrong but i don't found what it is. Any help will be appreciate !
thanks all,
回答1:
You need to add the initialization to your entity:
e.g:
public function __construct()
{
$this->image = new EmbeddedFile();
}
来源:https://stackoverflow.com/questions/52498466/vichuploaderbundle-propertyaccessor-requires-a-graph-of-objects-or-arrays-to-o