Skip to content

Instantly share code, notes, and snippets.

@NicolasBadey
Last active December 31, 2015 17:19
Show Gist options
  • Save NicolasBadey/8019798 to your computer and use it in GitHub Desktop.
Save NicolasBadey/8019798 to your computer and use it in GitHub Desktop.
DoctrineListener - IIM

dans app/config/config.yml

article.listener:
    class: IIM\BlogBundle\Listener\ArticleListener
    arguments: ["@service_container"]
    tags :
      - { name: doctrine.event_listener, event: prePersist }

IIM\BlogBundle\Listener

<?php

namespace iim\BlogBundle\Listener;

use Doctrine\ORM\Event\LifecycleEventArgs;
use iim\BlogBundle\Entity\Article;

class ArticleListener {


    protected $container;


    public function __construct($container)
    {
        $this->container = $container;
    }


    public function prePersist(LifecycleEventArgs $args)
    {
        $entity = $args->getObject();

        //agir sur un article
        if ($entity instanceof Article) {
            $user = $this->container->get('security.context')->getToken()->getUser();
            $entity->setAuthor($user);
        }

    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment