Doctrine-PHPCR-ODM Event doesn't fire

167 Views Asked by At

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}
2

There are 2 best solutions below

2
Saman Mohamadi On BEST ANSWER

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.
2
Julien Bourdic On
`/** 
  * @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.