【发布时间】:2019-03-27 18:36:30
【问题描述】:
我尝试在 UserService 实体中保存用户字段的初始值。原因是,我在 EasyAdminBundle 中使用了这个实体,当我构建表单时,我想为 user_id 设置一个默认值(ManyToOne to 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();
}
【问题讨论】:
标签: symfony doctrine-orm symfony4 symfony2-easyadmin easyadmin