【问题标题】:How to activate Gedmo Ip_Traceable in a Symfony 2.7 project?如何在 Symfony 2.7 项目中激活 Gedmo Ip_Traceable?
【发布时间】:2026-01-19 17:20:07
【问题描述】:

在我的 Symfony 2.7 项目中,我使用 StofDoctrineExtension‐Bundle。我已经声明了一些实体。他们使用 Timestampable、Loggable 和 Sluggable Gedmo 行为。我通过阅读 Symfony 官方文档中的 this tutorial 来安装它。当我通过 Sonata Admin 或我自己的用例创建某个实体时,它可以完美运行。

现在我想使用 Gadmo IpTraceable 行为。我通过添加两个属性 ipCreatoripUdater 更改了我的实体声明 ORM YAML 文件:

AppBundle\Entity\Vote:
  type: entity
  table: te_vote_vot
  id:
    id:
      type: bigint
      column: vot_id
      generator:
        strategy: AUTO
      options:
        unsigned: true
        comment: Identifiant du classement
  fields:
    created:
      type: datetime
      nullable: false
      column: vot_creation
      options:
        comment: Creation date
      gedmo:
        timestampable:
          on: create
    ipCreator:
      type: string
      length: 45
      nullable: true
      column: vot_ip_creator
      options:
        comment: IP du voteur
      gedmo:
        ipTraceable:
          on: create
    ipUpdater:
      type: string
      length: 45
      nullable: true
      column: vot_ip_updater
      options:
        comment: IP du voteur en cas de modification
      gedmo:
        ipTraceable:
          on: update
    tracker:
      type: guid
      nullable: false
      column: vot_tracker
      options:
        comment: Tracker GGUID du voteur
    point:
      type: smallint
      nullable: false
      column: vot_point
      options:
        comment: Nombre de point que rapporte ce vote
  [...]

Timestampable 行为被称为 created 属性已很好地初始化,但 ipCreator 不是并保持 null

为了激活 Timestampable,我在 config.yml 文件中添加了一些行。

stof_doctrine_extensions:
    default_locale: fr_FR
    orm:
        default:
            timestampable: true
            sluggable: true
            loggable: true

我尝试添加iptraceable: trueipTraceable: trueip_traceable: true。它不起作用 iptraceable、ipTraceable、ip_traceable 不是配置项。

我查看了DependancyInjection/Configuration.php 源文件。

$node
    ->useAttributeAsKey('id')
    ->prototype('array')
        ->children()
            ->scalarNode('translatable')->defaultFalse()->end()
            ->scalarNode('timestampable')->defaultFalse()->end()
            ->scalarNode('blameable')->defaultFalse()->end()
            ->scalarNode('sluggable')->defaultFalse()->end()
            ->scalarNode('tree')->defaultFalse()->end()
            ->scalarNode('loggable')->defaultFalse()->end()
            ->scalarNode('sortable')->defaultFalse()->end()
            ->scalarNode('softdeleteable')->defaultFalse()->end()
            ->scalarNode('uploadable')->defaultFalse()->end()
            ->scalarNode('reference_integrity')->defaultFalse()->end()
        ->end()
    ->end()
;

这些词都没有激活 IPTraceable 行为。 ipCreator 属性保持为空。我正在搜索一段时间,阅读文档,在网络上搜索。而且我没有找到任何解决方案。

但是in documentation,我读到了这一行:

IpTraceable 还不能作为 Symfony2 的 Bundle 提供,以及 所有其他扩展。

我的英语不流利,但我知道我添加以“手动”激活它,而不是像 Bundle。但我不知道该怎么做。

如何使用这个 IpTracable Doctrine Extensions ?

【问题讨论】:

  • 也许您可以尝试使用事件订阅者,就像在他们的文档中一样(即使 ipTraceable 目前不在捆绑包中,他们也有 sf2 项目中的基本实现示例)-link
  • 感谢@IlyaYarkovets 提供此链接!我没看到这一段。今晚我将与事件订阅者一起尝试,我会回来转发结果。
  • 是的,它适用于事件订阅者。

标签: php symfony doctrine-orm


【解决方案1】:

方法一:有事件订阅者

正如@Ilya 建议的那样,我尝试使用symfony documentation 中的事件订阅者。

但是配置服务很麻烦,因为我用的是yml。

所以这里是services.xml 文件的 yaml 版本:

#app/config/services.yml
gedmo_doctrine_extensions.listener.ip_traceable:
    class: Gedmo\IpTraceable\IpTraceableListener
    public: false
    tags:
        - { name: doctrine.event_subscriber, connection: default }
    calls:
        - [setAnnotationReader, [ "@annotation_reader"]]

alterphp_doctrine_extensions.event_listener.ip_trace:
    class: %alterphp_doctrine_extensions.event_listener.ip_trace.class%
    scope: request
    arguments:
        - @gedmo_doctrine_extensions.listener.ip_traceable
        - @?request
    tags:
        - { name: kernel.event_subscriber}

方法2:等待合并Pull-Request #233

自 2014 年 3 月以来,有一个 pull-request #233 添加对 IpTraceable Gedmo 扩展的支持。您可以等待 pull-request 与 master 合并。不幸的是,@stof 似乎没有时间做这个项目。不知道什么时候完成。

方法3:使用AlterPHP fork!

您可以从 composer.json 中删除 Stof/StofDoctrineExtensions 并使用 alterphp fork

别忘了调用 iptraceable-listener 分支!

您必须通过编辑您的 app/config/config.yml 文件来激活 ip_traceable 订阅者

stof_doctrine_extensions:
    orm:
        default:
            ip_traceable: true

例如,这是我的 config.yml 文件:

stof_doctrine_extensions:
    default_locale: fr_FR
    orm:
        default:
            timestampable: true
            sluggable: true
            loggable: true
            ip_traceable: true

我尝试了方法 1 和 2,都可以完美运行。 (但当然不能在一起)。

所以选择吧;)

【讨论】:

  • 在使用SF3的方法1中,您需要在@之前添加简单的引号:'@gedmo_doctrine_extensions.listener.ip_traceable'