我尝试在UserService实体中保存用户字段的初始值。原因是,我在EasyAdminBundle中使用此实体,并且在构建表单时,我想为user_id(将ManyToOne更改为User实体)设置默认值。
...但是保存期间我仍然看不到更改。
class UserServiceRepository extends ServiceEntityRepository
{
protected $_em;
public function __construct(RegistryInterface $registry)
{
$this->_em = $this->entityManager;
parent::__construct($registry, UserService::class);
}
// I override save method:
public function save(UserService $userService)
{
// Get current user from security:
$user = $this->get('security.token_storage')->getToken()->getUser();
// set to useService...
$userService->setUser($user);
// and persist & flush:
$this->_em->persist($userService);
$this->_em->flush();
}
答案 0 :(得分:1)
AbstractMojo
您要覆盖父级中不存在的方法,ServiceEntityRepository和EntityRepository中都没有保存方法。那么,您要做什么的重点是什么,为什么要在服务存储库中设置默认的user_id?
更新:
// I override save method:
监听器:
services:
my.listener:
class: UserServiceListener
arguments:
- "@security.token_storage"
tags:
- { name: doctrine.event_listener, event: prePersist }