如何在Symfony 2.7项目中激活Gedmo Ip_Traceable


How to activate Gedmo Ip_Traceable in a Symfony 2.7 project?

在我的Symfony 2.7项目中,我使用StofDoctrineExtension‐Bundle。我已经申报了一些实体。他们使用Timestamble、Logable和Sluggable Gedmo行为。我通过阅读Symfony官方文档中的本教程安装了它。当我通过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
  [...]

可时间戳的行为被称为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都不是配置术语。

我查看了DependencyInjection/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 Behavior。ipCreator属性保持为空。我正在搜索一段时间,阅读文档,在网上搜索。我找不到任何解决方案。

但在文件中,我读到了这一行:

IpTraceable尚未作为Symfony2的捆绑包提供所有其他扩展。

我的英语不流利,但我知道我添加是为了"手动"激活它,而不是像Bundle一样。但我不知道怎么做。

如何使用此IpTracable条令扩展?

方法1:使用事件订阅者

正如@Ilya所建议的,我尝试了像symfony文档中那样使用事件订阅者。

但是很难配置服务,因为我使用的是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:等待合并请求#233

自2014年3月以来,有一个请求#233添加了对IpTraceableGedmo扩展的支持。您可以等待pull请求与master合并@遗憾的是,stof似乎没有时间做这个项目。我不知道什么时候能完成。

方法3:使用AlterPHP fork

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

别忘了打电话给iptraceable监听器分支!

您必须通过编辑您的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,结果都很好。(当然不是一起)。

因此,请自行选择;)