Skip to content

Instantly share code, notes, and snippets.

@akiletour
Last active August 29, 2015 13:55
Show Gist options
  • Save akiletour/8688312 to your computer and use it in GitHub Desktop.
Save akiletour/8688312 to your computer and use it in GitHub Desktop.
Symfony2: Update the slug of a product with ID-TITLE (postPersist)
<?php
// src/Aki/MyBundle/Entity/Product.php
/**
* [...]
* @ORM\HasLifecycleCallbacks()
*/
class Product
{
[...]
services:
aki.slug:
class: Aki\MyBundle\Listener\SlugUpdate
tags:
- { name: doctrine.event_listener, event: postPersist }
<?php
// src/Aki/MyBundle/Listener/SlugUpdate.php
namespace Aki\MyBundle\Listener;
use Aki\MyBundle\Entity\Product;
use Doctrine\ORM\Event\LifecycleEventArgs;
class SlugUpdate
{
public function postPersist(LifecycleEventArgs $args)
{
$entity = $args->getEntity();
$em = $args->getEntityManager();
if ($entity instanceof Product) {
$entity->setSlug(null);
$em->persist($entity);
$em->flush();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment