标题说明了一切。我希望能够从我的实体中的prePersist函数中访问该表单。像这样:
/** @ORM\PrePersist */
public function prePersist()
{
//Symfony 1.4 way
$params = sfContext::getInstance()->getRequest()->getParameterHolder()->getAll();
$this->gps84Long=$params['coordinate']['gps_84_lat_degrees'];
}
我需要这个的原因是因为表单值没有映射到数据库字段。我需要将2-3个表单字段组合成一个值,放在我的实体中的单个字段中。
EDIT-为提供的答案添加.yml添加:
services:
my.doctrine.subscriber:
class: myCompany\myBundle\Models\MyDoctrineSubscriber
arguments: [@form.factory,"@request_stack"]
tags:
- { name: 'doctrine.event_subscriber', connection: 'default' }
答案 0 :(得分:1)
我的建议是创建一个Doctrine subscriber并注入你想要的任何服务(xml示例):
service.xml中
<service id="my.doctrine.subscriber" class="%my.doctrine.subscriber.class%">
<argument type="service" id="form.factory" />
<argument type="service" id="request_stack" />
<tag name="doctrine.event_subscriber" connection="default" />
</service>
MyDoctirneSubscriber
class MyDoctirneSubscriber implements EventSubscriber
{
private $formFactory;
private $requestStack;
/**
* @param ContainerInterface $container
*/
public function __construct(FormFactoryInterface $formFactory, RequestStack $requestStack)
{
$this->formFactory = $formFactory;
$this->requestStack = $requestStack;
}
/**
* @return array
*/
public function getSubscribedEvents()
{
return [
'prePersist',
...
];
}
/**
* @param LifecycleEventArgs $eventArgs
*/
public function prePersist(LifecycleEventArgs $eventArgs)
{
$entityManager = $eventArgs->getEntityManager();
$entity = $eventArgs->getEntity();
if ($entity instanceof MyEntity) {
$request = $this->requestStack->getCurrentRequest();
$form = $this->formFactory->create(TYPE, DATA_OR_NULL, OPTIONS);
$form->handleRequest($request);
....
}
}
....
}