symfony - 使用监听器以表单形式注入的 Symfony 更改对象
问题描述
我正在使用 Symfony 3.4,我有一件奇怪的事情要做,我会尽量解释清楚。
我有一个实体Website
和一个WebsiteFormType
,在这个WebsiteFormType
我有一个像这样的监听器:(buildForm()
在formType的第一行)
$builder->addEventSubscriber(new WebsiteListener();
在此侦听器中,我需要检查提交表单时何时更新特定值,如果更新此值,我需要复制我的对象,例如当我更新我的网站并将url
from更改http://xxx.xx
为http://yyyy.yy
我复制我的第一个网站创建第二个而不是仅仅更新字段url
。
在此侦听器中,我使用preSubmit
and postSubmit
。
我的问题是,当我更改网址时,我需要更改链接到表单的对象网站。
如果http://xxx.xx
是 WebsiteA 并且http://yyyy.yy
是 WebsiteB,当我提交 WebsiteA 并更改 url 时,我需要将链接到表单的 WebsiteA 对象从 WebsiteA 更改为 WebsiteB....
这是因为如果我在那之后重新验证表单,它是经过验证的 WebsiteB 而不是 WebsiteA。
不知道你是否理解我的问题:) 谢谢!
解决方案
尝试使用Doctrine EventSubscriber,例如:
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)
}
}
}
推荐阅读
- python - Python Multiprocesssing:当我向 ProcessPoolExecutor 提交对象列表时,提交的是副本还是参考?
- kdb - 如何用另一个数字替换列表中的“x”?
- python - 为什么类属性中的列表实例化后还是类属性,而不是实例属性?
- compatibility - 需要将 SoapAPI 转移到 RestAPI,有什么可用的吗?它是什么样子的?
- javascript - 未捕获的类型错误:$(...).barrating 不是函数 - Rails / Turbolinks
- sql - Conflict on two PostgreSQL transactions with serializable isolation level
- react-native - Use redux-persist instead of local DB for big data? React Native
- jquery - 确保控制器方法实际上是由 ajax 或 mvc 中的 POST 请求调用的
- jquery - Minify Arrays with chrome set
- python-3.x - 如何在 if 语句中正确构建“分数小于 10 或大于 90”?