symfony - 用户对我在 EasyAdmin3 和 symfony5 上的成就的问题
问题描述
这个问题出现在 Symfony 5 上。我使用 EasyAdmin v3 包创建了站点的管理端。当我尝试添加实现时出现我的问题,它给了我以下错误消息。
https://i.stack.imgur.com/RXpLr.png
它说我的用户不能为空,但问题是我正在尝试使用管理员帐户添加实现,因此我希望他们考虑到我以管理员身份登录并将自己从该帐户中实现。下面是我的代码我的实现实体。
<?php
namespace App\Entity;
use App\Repository\RealisationRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\File;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
/**
* @ORM\Entity(repositoryClass=RealisationRepository::class)
* @Vich\Uploadable
*/
class Realisation
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $nom;
/**
* @ORM\Column(type="datetime")
*/
private $createdAt;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $dateRealisation;
/**
* @ORM\Column(type="text")
*/
private $description;
/**
* @ORM\Column(type="boolean")
*/
private $portfolio;
/**
* @ORM\Column(type="string", length=255)
*/
private $slug;
/**
* @ORM\Column(type="string", length=255)
*/
private $file;
/**
* @var File
* @Vich\UploadableField(mapping="Realisation",fileNameProperty="file")
*/
private $imageFile;
/**
* @ORM\ManyToOne(targetEntity=User::class, inversedBy="realisations")
* @ORM\JoinColumn(nullable=false)
*/
private $user;
/**
* @ORM\ManyToMany(targetEntity=Categorie::class, inversedBy="realisations")
*/
private $categorie;
public function __construct()
{
$this->categorie = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getNom(): ?string
{
return $this->nom;
}
public function setNom(string $nom): self
{
$this->nom = $nom;
return $this;
}
public function getCreatedAt(): ?\DateTimeInterface
{
return $this->createdAt;
}
public function setCreatedAt(\DateTimeInterface $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
public function getDateRealisation(): ?\DateTimeInterface
{
return $this->dateRealisation;
}
public function setDateRealisation(?\DateTimeInterface $dateRealisation): self
{
$this->dateRealisation = $dateRealisation;
return $this;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(string $description): self
{
$this->description = $description;
return $this;
}
public function getPortfolio(): ?bool
{
return $this->portfolio;
}
public function setPortfolio(bool $portfolio): self
{
$this->portfolio = $portfolio;
return $this;
}
public function getSlug(): ?string
{
return $this->slug;
}
public function setSlug(string $slug): self
{
$this->slug = $slug;
return $this;
}
public function getFile(): ?string
{
return $this->file;
}
public function setFile(string $file): self
{
$this->file = $file;
return $this;
}
public function getUser(): ?User
{
return $this->user;
}
public function setUser(?User $user): self
{
$this->user = $user;
return $this;
}
/**
* @return Collection|Categorie[]
*/
public function getCategorie(): Collection
{
return $this->categorie;
}
public function addCategorie(Categorie $categorie): self
{
if (!$this->categorie->contains($categorie)) {
$this->categorie[] = $categorie;
}
return $this;
}
public function removeCategorie(Categorie $categorie): self
{
$this->categorie->removeElement($categorie);
return $this;
}
/**
* @return File
*/
public function getImageFile(): ?File
{
return $this->imageFile;
}
/**
* @param File $imageFile
*/
public function setImageFile(?File $imageFile = null)
{
$this->imageFile = $imageFile;
if(null !== $imageFile){
$this->dateRealisation = new \DateTime();
}
}
}
我的实现CrudController
<?php
namespace App\Controller\Admin;
use App\Entity\Realisation;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
use EasyCorp\Bundle\EasyAdminBundle\Field\AssociationField;
use EasyCorp\Bundle\EasyAdminBundle\Field\BooleanField;
use EasyCorp\Bundle\EasyAdminBundle\Field\DateTimeField;
use EasyCorp\Bundle\EasyAdminBundle\Field\ImageField;
use EasyCorp\Bundle\EasyAdminBundle\Field\IntegerField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextEditorField;
use EasyCorp\Bundle\EasyAdminBundle\Field\IdField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextareaField;
use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
use Symfony\Component\Validator\Constraints\DateTime;
use Vich\UploaderBundle\Form\Type\VichFileType;
class RealisationCrudController extends AbstractCrudController
{
public static function getEntityFqcn(): string
{
return Realisation::class;
}
public function configureFields(string $pageName): iterable
{
return [
IntegerField::new('id','ID')->onlyOnIndex(),
TextField::new('nom'),
TextEditorField::new('description'),
DateTimeField::new('createdAt'),
DateTimeField::new('dateRealisation'),
TextField::new('slug'),
BooleanField::new('portfolio'),
TextareaField ::new('imageFile')
->setFormType(VichFileType::class)
->setLabel('Image'),
];
}
}
解决方案
您是否希望用户始终是登录创建您的Realisation
实体的用户?
实现此目的的一种简单方法是使用事件自动将用户设置为您的登录用户。
一个例子是使用 EventSubscriber:
class DoctrineSubscriber implements EventSubscriber
private $tokenStorage;
public function __construct(
TokenStorageInterface $tokenStorage
) {
$this->tokenStorage = $tokenStorage;
}
public function getSubscribedEvents(): array
{
return [
Events::prePersist => 'prePersist',
];
}
public function prePersist(OnFlushEventArgs $args)
{
$entity = $args->getObject();
if ($entity instanceof Realisation) {
$user = $this->tokenStorage->getToken()->getUser();
if($user){
$entity->setUser($user);
}else{
//throw an exception, no user logged in
}
}
}
}
并且不要忘记在 services.yaml 中定义您的事件订阅者
#services.yaml
App\EventSubscriber\DoctrineSubscriber:
tags:
- { name: doctrine.event_subscriber }
另一种方法是从您的 EasyAdmin AbstractCrudController 覆盖 createEntity 方法:
public function createEntity(string $entityFqcn)
{
$realisation = new Realisation();
$realisation->setUser($this->getUser());
return $realisation;
}
推荐阅读
- puppeteer - 从 puppeteer 脚本中的 csv 读取数据并将数据传递到某些网站以自动填写表格,还可以通过 puppeteer 在其他网站上上传图像
- html - Flex 粘性页脚在窗口调整大小时与 flex 兄弟重叠(高度缩小)
- branch-prediction - 为什么不能在另一个过程中训练分支预测器。
- react-native - 如何在本机反应中永久获取移动设备(iOS和android)的uuid
- certificate - 具有多个帐户的 Twilio Push 凭据
- javascript - 没有为 emulateTransitionEnd 触发事件
- excel - Excel VBA 范围直到最后一个可用单元格
- android - 从 JSON 获取数据?
- python - pandas 和 numpy 的意思不同
- android - 奥利奥:阻止通知在滑动时被删除