使用侦听器在表单中注入Symfony change对象
我正在使用Symfony 3.4,我有一件奇怪的事情要做,我会尽量解释清楚 我有一个实体使用侦听器在表单中注入Symfony change对象,symfony,Symfony,我正在使用Symfony 3.4,我有一件奇怪的事情要做,我会尽量解释清楚 我有一个实体Website和一个WebsiteFormType,在这个WebsiteFormType中,我有一个监听器如下:(在formType中的buildForm()的第一行中) 在这个监听器中,我需要检查表单提交时特定值何时更新,如果该值更新,我需要复制我的对象,例如,当我更新我的网站并从http://xxx.xx至http://yyyy.yy我复制了我的第一个网站来创建第二个网站,而不是仅仅更新字段url 在这个
Website
和一个WebsiteFormType
,在这个WebsiteFormType
中,我有一个监听器如下:(在formType中的buildForm()的第一行中)
在这个监听器中,我需要检查表单提交时特定值何时更新,如果该值更新,我需要复制我的对象,例如,当我更新我的网站并从http://xxx.xx
至http://yyyy.yy
我复制了我的第一个网站来创建第二个网站,而不是仅仅更新字段url
在这个监听器中,我使用了preSubmit
和postSubmit
我的问题是,当我更改url时,我需要更改链接到表单的对象网站
如果http://xxx.xx
是一个网站,http://yyyy.yy
是网站B,当我提交网站A并更改url时,我需要将链接到表单的网站对象从网站A更改为网站B
这是为了如果我在那之后重新验证表单,它是经过验证的WebsiteB,而不是WebsiteA
不知道你是否理解我的问题:)谢谢 尝试使用订阅服务器如:
class OrderListener implements EventSubscriber {
protected $statusChanges = false;
public function getSubscribedEvents()
{
return array(
'preUpdate',
'postUpdate',
);
}
public function preUpdate(PreUpdateEventArgs $args)
{
$changeSet = $args->getEntityChangeSet();
foreach ($changeSet as $key => $arr) {
if ($key === 'status' && (int)$arr[0] !== (int)$arr[1]) {
$this->statusChanges = true;
}
}
}
public function postUpdate(LifecycleEventArgs $args)
{
$entity = $args->getObject();
if ($entity instanceof Order && $this->statusChanges) {
$repo = $args->getObjectManager()->getRepository(Action::class);
$action = new Action();
$action->setOrder($entity)
->setStatus($entity->getStatus())
->setCost($entity->getCost())
->setTimeAt(new \DateTime())
->setPoint($entity->getPoint())
->setDescription($entity->getDescription())
->setService($entity->getService())
;
$repo->persistAndFlush($action); // custom method, you can use $args->getObjectManager()->persist($action) and $args->getObjectManager()->flush($action)
}
}
}
看起来你是在一个月内解决的?
class OrderListener implements EventSubscriber {
protected $statusChanges = false;
public function getSubscribedEvents()
{
return array(
'preUpdate',
'postUpdate',
);
}
public function preUpdate(PreUpdateEventArgs $args)
{
$changeSet = $args->getEntityChangeSet();
foreach ($changeSet as $key => $arr) {
if ($key === 'status' && (int)$arr[0] !== (int)$arr[1]) {
$this->statusChanges = true;
}
}
}
public function postUpdate(LifecycleEventArgs $args)
{
$entity = $args->getObject();
if ($entity instanceof Order && $this->statusChanges) {
$repo = $args->getObjectManager()->getRepository(Action::class);
$action = new Action();
$action->setOrder($entity)
->setStatus($entity->getStatus())
->setCost($entity->getCost())
->setTimeAt(new \DateTime())
->setPoint($entity->getPoint())
->setDescription($entity->getDescription())
->setService($entity->getService())
;
$repo->persistAndFlush($action); // custom method, you can use $args->getObjectManager()->persist($action) and $args->getObjectManager()->flush($action)
}
}
}