Last active
August 29, 2015 13:55
-
-
Save akiletour/8688312 to your computer and use it in GitHub Desktop.
Symfony2: Update the slug of a product with ID-TITLE (postPersist)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// src/Aki/MyBundle/Entity/Product.php | |
/** | |
* [...] | |
* @ORM\HasLifecycleCallbacks() | |
*/ | |
class Product | |
{ | |
[...] | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
services: | |
aki.slug: | |
class: Aki\MyBundle\Listener\SlugUpdate | |
tags: | |
- { name: doctrine.event_listener, event: postPersist } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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