我尝试使此捆绑包与Form一起运行。
首先这是我的实体
<?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要求使用对象图或数组图进行操作,但是在尝试遍历属性“名称”处的路径“ image.name”时发现类型为“ NULL”。
我知道某事是错误的,但我没有发现那是什么。任何帮助将不胜感激!
谢谢,
答案 0 :(得分:1)
您需要将初始化添加到您的实体:
例如:
public function __construct()
{
$this->image = new EmbeddedFile();
}