且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在实体类中访问Symfony2全局参数

更新时间:2023-11-19 22:47:40

***做法是使用服务来保留您的实体.当调用 updateMyEntity() 服务方法时,这将注入容器并设置参数.

Best practice is to use a service to persist your entity. This one would inject the container and set your parameter when you call your updateMyEntity() service method.

在控制器内部(或您想要的任何内部):

Inside your controller (or whatever you want):

$user = new User('foo');
$user->setSomeProperty('bar');
$userService->update($user);

UserService内:

public function update(User $user) {
    $user->setSomeParameter($this->container->getParameter('value'));
    $this->em->persist($user);
}