/** * setRole * * @param LifecycleEventArgs $args */ public function setRole(LifeCycleEventArgs $args) { /** @var User $object */ $object = $args->getEntity(); if ($object instanceof AdvancedUserInterface) { $object->setRoles(User::ROLE_USER); } }
/** * Remove empty values after persisting, to avoid null 'Value' values in * the database. * * @param LifeCycleEventArgs $args * * @return void */ public function postPersist(LifeCycleEventArgs $args) { $entity = $args->getEntity(); $entityManager = $args->getEntityManager(); if ($entity instanceof ValueInterface && $entity->isEmpty()) { $entityManager->remove($entity); } }
private function setMaxRootSequence($item, LifeCycleEventArgs $event) { $em = $event->getEntityManager(); $qb = $em->createQueryBuilder(); $maxSequence = $qb->select('MAX(i.rootSequence)')->from(get_class($item), 'i')->getQuery()->getOneOrNullResult(); $sequenceNext = (int) $maxSequence[1] + 1; if (is_null($item->getParent())) { $item->setRootSequence($sequenceNext); } }
/** * @param LifecycleEventArgs $args */ public function prePersist(LifeCycleEventArgs $args) { /** @var AdvancedUserInterface $object */ $object = $args->getEntity(); if ($object instanceof AdvancedUserInterface) { $salt = md5(uniqid(null, true)); $object->setSalt($salt); $encoder = $this->encoder->getEncoder($object); $encodedPassword = $encoder->encodePassword($object->getPlainPassword(), $object->getSalt()); $object->setPassword($encodedPassword); $object->setLastLogin(new \DateTime(date('Y-m-d H:i:s'))); } }
/** * @param LifecycleEventArgs $args */ public function prePersist(LifeCycleEventArgs $args) { /** @var UrlInterface $object */ $object = $args->getEntity(); if ($object instanceof UrlInterface) { $urlUtility = new UrlUtility(); $processedUrl = $urlUtility->process($object->getMetaUrl()); $found = $args->getEntityManager()->getRepository(get_class($object))->findOneBy(['metaUrl' => $processedUrl]); if ($found) { throw new \LogicException('Given URL already exists'); } $object->setMetaUrl($processedUrl); } }
/** * postPersist * * @param LifecycleEventArgs $args */ public function postPersist(LifeCycleEventArgs $args) { /** @var Node $parent */ /** @var Node $object */ $dm = $args->getDocumentManager(); $object = $args->getDocument(); if ($object instanceof NodeInterface) { if ($parent = $object->getParent()) { $parent->addChild($object); $dm->persist($parent); $dm->flush(); } } }
public function prePersist(LifeCycleEventArgs $args) { $entity = $args->getEntity(); if ($entity instanceof Post || $entity instanceof Comment) { // is authentication information available? if (null !== $this->tokenStorage->getToken()) { // get User $user = $this->tokenStorage->getToken()->getUser(); if (is_object($user)) { $entity->setAuthorEmail($user->getEmail()); } } } }
/** * @param LifecycleEventArgs $args */ private function updateChildren(LifeCycleEventArgs $args) { $dm = $args->getDocumentManager(); /** @var NodeInterface $object */ /** @var Category $parent */ /** @var Category $child */ $object = $args->getDocument(); if ($object instanceof NodeInterface) { // var_dump($object->getParent()); if ($parent = $object->getParent()) { foreach ($parent->getChildren() as $child) { if ($child->getId() == $object->getId()) { $parent->removeChild($child); } } $parent->addChild($object); $dm->persist($parent); $dm->flush(); } } }
/** * PreUpdate */ public function preUpdate(LifeCycleEventArgs $args) { $entity = $args->getEntity(); $entity->doPreUpdate(); }