1

I have a doctrine-phpcr-odm document named article,I want to slugify a field before updating each article.
The event fires for doctrine-orm entities but dosn't fire for doctrine-phpcr-odm documents!

class ArticlePreUpdateListener
{
    public function preUpdate(LifecycleEventArgs $args)
    {
        var_dump($args);
    }
}

article.pre_update.listener:
    class: AppBundle\EventListener\ArticlePreUpdateListener
    tags:
        - { name: doctrine.event_listener, event: preUpdate}
Saman Mohamadi
  • 4,454
  • 4
  • 38
  • 58

2 Answers2

1

According to Docs, Doctrine-PHPCR-ODM events works the same way as for Doctrine ORM events. The only differences are:

  • use the tag name doctrine_phpcr.event_listener resp. doctrine_phpcr.event_subscriber instead of doctrine.event_listener;
  • expect the argument to be of class Doctrine\Common\Persistence\Event\LifecycleEventArgs.
Saman Mohamadi
  • 4,454
  • 4
  • 38
  • 58
  • does that mean you solved your question? or does it not work with the doctrine_phpcr.event_subscriber tag? (note you can add both tags if its the same behaviour for both) – dbu Sep 23 '16 at 13:19
  • @dbu Yes using doctrine_phpcr.event_subscriber tag Solved the problem.tnx – Saman Mohamadi Sep 23 '16 at 14:29
-1
`/** 
  * @Document 
  */
class Article
{
    [...]

    /**
     * @PreUpdate
     * @PrePersist
     */
    public function slugifiyField()
    {
       $this->yourField = yourSlugifyFunction($this->yourField);
    }
}

Then, add a function with a preUpdate annotation (I've added PrePersist to slugify when article is created too)

Edit : According to your comment, I removed HasLifeCycleCallback annotation, but it looks you can use Pre/PostUpdate annotations directly within document entity.

Julien Bourdic
  • 1,398
  • 1
  • 12
  • 29
  • Contrary to the ORM, PHPCR-ODM does not use the `@HasLifecycleCallbacks` marker(http://docs.doctrine-project.org/projects/doctrine-phpcr-odm/en/latest/reference/events.html#lifecycle-callbacks). – Saman Mohamadi Sep 23 '16 at 09:59
  • Sorry, I based my response on http://docs.doctrine-project.org/projects/doctrine-mongodb-odm/en/latest/reference/annotations-reference.html#haslifecyclecallbacks I edited my answer – Julien Bourdic Sep 23 '16 at 10:12