Created
June 18, 2011 21:03
-
-
Save cordoval/1033504 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
/* | |
* based on FOS\UserBundle\Entity | |
*/ | |
namespace HBK\ScavengerHuntBundle\Entity; | |
use Doctrine\Common\EventSubscriber; | |
use Doctrine\ORM\Events; | |
use Doctrine\ORM\Event\LifecycleEventArgs; | |
use Doctrine\ORM\Event\PreUpdateEventArgs; | |
use Doctrine\ORM\Event\OnFlushEventArgs; | |
use Doctrine\ORM\PersistentCollection; | |
// use FOS\UserBundle\Model\UserInterface; | |
use Symfony\Component\DependencyInjection\ContainerInterface; | |
use HBK\ScavengerHuntBundle\Entity\User; | |
use HBK\ScavengerHuntBundle\Entity\Badge; | |
/** | |
* Doctrine ORM listener | |
* - awards 1-up badge on User create | |
*/ | |
class UserListener implements EventSubscriber | |
{ | |
/** | |
* @var ContainerInterface | |
*/ | |
private $container; | |
/** | |
* Constructor | |
* | |
* @param ContainerInterface $container | |
*/ | |
public function __construct(ContainerInterface $container) | |
{ | |
$this->container = $container; | |
} | |
public function getSubscribedEvents() | |
{ | |
return array( | |
Events::prePersist, | |
Events::preUpdate, | |
Events::onFlush | |
); | |
} | |
public function prePersist(LifecycleEventArgs $args) | |
{ | |
error_log( 'PRE PERSIST'); | |
$this->award1Up($args); | |
} | |
public function preUpdate(PreUpdateEventArgs $args) | |
{ | |
error_log( 'PRE UPDATE'); | |
$entity = $args->getEntity(); | |
$em = $args->getEntityManager(); | |
} | |
public function onFlush(OnFlushEventArgs $args) | |
{ | |
error_log( 'ON FLUSH'); | |
$entities = array(); | |
$em = $args->getEntityManager(); | |
$uow = $em->getUnitOfWork(); | |
foreach ($uow->getScheduledEntityInsertions() AS $entity) { | |
$entities[] = $entity; | |
} | |
foreach ($uow->getScheduledEntityUpdates() AS $entity) { | |
$entities[] = $entity; | |
} | |
foreach ($uow->getScheduledCollectionUpdates() AS $entity) { | |
$entities[] = $entity; | |
} | |
foreach ($uow->getScheduledCollectionDeletions() AS $entity) { | |
$entities[] = $entity; | |
} | |
foreach ($entities as $entity) { | |
error_log(get_class($entity)); | |
error_log(get_parent_class($entity)); | |
if ($entity instanceof PersistentCollection){ | |
foreach($entity as $element) { | |
// if( $element instanceof Badge) | |
error_log("BADGE: " . $element->getName() ); | |
} | |
} | |
if ($entity instanceof User) { | |
error_log("USER: " . $entity->getUsername() . " badges count: " . $entity->getBadges()->count()); | |
} | |
} | |
// $this->award1Up($args); | |
} | |
private function award1Up(LifecycleEventArgs $args) | |
{ | |
$entity = $args->getEntity(); | |
$em = $args->getEntityManager(); | |
if ($entity instanceof User) { | |
$badge1Up = $em->getRepository('HBKScavengerHuntBundle:Badge') | |
->findOneBySlug('1-up-power-up'); | |
$entity->addBadges($badge1Up); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment