【发布时间】:2015-06-08 16:05:19
【问题描述】:
在 Symfony2 项目中,我正在使用 Loggable Doctrine 扩展。
我saw 说有一个 LoggableListener。
当可记录实体中的(可记录)字段发生更改时,是否确实存在触发事件?如果是这样,有没有办法获取触发它的字段列表?
我正在想象一个实体的情况,假设有 10 个字段,其中 3 个可记录。对于这 3 个中的每一个,如果它们的值发生变化,我想执行一些操作,因此如果它们中的 3 个发生变化,则将执行 3 个操作。
有什么想法吗?
谢谢!
编辑
阅读下面的评论并阅读有关教义事件的文档后,我理解有 3 个选项:
1) 直接在实体级别使用 lifecycle callbacks 甚至 with arguments 如果我使用的是学说 >2.4
2) 我可以listen and subscribe to Lifecycle Events,但在这种情况下,文档说"Lifecycle events are triggered for all entities. It is the responsibility of the listeners and subscribers to check if the entity is of a type it wants to handle."
3) 执行您的建议,即使用Entity listener,您可以在实体级别定义要“附加”到类的侦听器。
即使第一个解决方案看起来更简单,我读到“您也可以使用此侦听器来实现对所有已更改字段的验证。这比在需要调用昂贵的验证时使用生命周期回调更有效”。什么是“昂贵的验证”。
在我的情况下,我必须执行的操作类似于“如果实体 Y 的字段 X 发生更改,而不是在通知表上添加一条通知,说明“用户 Z 将 X(Y) 的值从 A 更改为 B”
考虑到我有大约 1000 个这样的字段,哪种方法最合适?
EDIT2
为了解决我的问题,我尝试将 service_container 服务注入到侦听器中,以便我可以访问调度程序来调度一个新事件,该事件可以执行我需要的新实体的持久化。但我该怎么做呢?
我尝试了通常的方式,我在service.yml中添加以下内容
app_bundle.project_tolereances_listener:
class: AppBundle\EventListener\ProjectTolerancesListener
arguments: [@service_container]
当然,我在监听器中添加了以下内容:
protected $container;
public function __construct(ContainerInterface $container)
{
$this->container = $container;
}
但我得到以下信息:
Catchable Fatal Error: Argument 1 passed to AppBundle\ProjectEntityListener\ProjectTolerancesListener::__construct() must be an instance of AppBundle\ProjectEntityListener\ContainerInterface, none given, called in D:\provarepos\user\vendor\doctrine\orm\lib\Doctrine\ORM\Mapping\DefaultEntityListenerResolver.php on line 73 and defined
有什么想法吗?
【问题讨论】:
标签: symfony doctrine-orm doctrine-extensions