实体prePersist中的Symony2访问表单?
Symony2 Access form in entity prePersist?
标题说明了一切。我希望能够从实体中的 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 个表单字段合并为一个值,以便放入我的实体中的单个字段中。
编辑-为提供的答案添加了 .yml 添加:
services:
my.doctrine.subscriber:
class: myCompany\myBundle\Models\MyDoctrineSubscriber
arguments: [@form.factory,"@request_stack"]
tags:
- { name: 'doctrine.event_subscriber', connection: 'default' }
我的建议是创建一个 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);
....
}
}
....
}
标题说明了一切。我希望能够从实体中的 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 个表单字段合并为一个值,以便放入我的实体中的单个字段中。
编辑-为提供的答案添加了 .yml 添加:
services:
my.doctrine.subscriber:
class: myCompany\myBundle\Models\MyDoctrineSubscriber
arguments: [@form.factory,"@request_stack"]
tags:
- { name: 'doctrine.event_subscriber', connection: 'default' }
我的建议是创建一个 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);
....
}
}
....
}