如何解决如何注册默认登录用户?
**Context: ** 我有 2 个关联实体,分别是“persona”和“ingreso”。
TextField::new('person','Person')
->formatValue(function ($value) {
return $value = $this->getUser();
})
->hideOnForm()
但是:这在数据库中作为 Null 值到达。
这就是为什么我尝试捕获用户并将其从实体中保存,但我不知道正确的方法。
解决方法
您正在使用 ->hideOnForm,它会删除表单中的字段,因此不会发送关于 person 的任何内容。
有多种方法可以做您想做的事,包括类似的答案,例如对您的用户进行隐藏选择,但我认为这不是一个好的解决方案。
你考虑过使用 Event 吗?
在您的情况下,您可以使用 Doctrine 事件或 EasyAdmin 事件进行监听。
<?php
namespace App\EventSubscriber;
use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityUpdatedEvent;
//... other imports
class EasyAdminSubscriber implements EventSubscriberInterface
{
private $tokenStorage
public function __construct(TokenStorageInterface $tokenStorage)
{
$this->tokenStorage = $tokenStorage
}
public static function getSubscribedEvents()
{
return [BeforeEntityUpdatedEvent => ['beforeEntityUpdatedEvent'],];
}
public function beforeEntityUpdatedEvent(BeforeEntityUpdatedEvent $event)
{
$entity = $event->getEntityInstance();
if ($entity instanceof YourEntityYouWantToListenTo) {
$entity->setPerson($this->tokenStorage->getToken()->getUser());
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。