public function preRemove(LifecycleEventArgs $args) { $entity = $args->getObject(); if ($entity instanceof Page) { $menuElements = $args->getObjectManager()->getRepository('SKCMSCoreBundle:MenuElement')->findBy(['entityClass' => '\\' . get_class($entity), 'entityId' => $entity->getId()]); foreach ($menuElements as $menuElement) { $args->getObjectManager()->remove($menuElement); } } }
/** * @param LifecycleEventArgs $args */ public function postPersist(LifecycleEventArgs $args) { $entity = $args->getObject(); if ($entity instanceof Dog || $entity instanceof Member || $entity instanceof Affiliate) { if (!$entity->getRegNumber()) { $entity->setRegNumber($this->generateRegNumber($entity->getId())); $tableName = $args->getObjectManager()->getClassMetadata(get_class($entity))->getTableName(); $dbConnection = $args->getObjectManager()->getConnection(); $dbConnection->executeUpdate('UPDATE ' . $tableName . ' SET regNumber = ? WHERE id = ?', array($entity->getRegNumber(), $entity->getId())); } } }
public function PostUpdate(LifecycleEventArgs $args) { $entity = $args->getObject(); $em = $args->getObjectManager(); $classMetaData = $em->getClassMetadata(get_class($entity)); foreach ($classMetaData->associationMappings as $associationMappingKey => $associationMappingDatas) { if ($associationMappingDatas['type'] == \Doctrine\ORM\Mapping\ClassMetadataInfo::ONE_TO_MANY) { $repo = $em->getRepository($associationMappingDatas['targetEntity']); $associated = $repo->findBy([$associationMappingDatas['mappedBy'] => $entity]); $newAssociated = call_user_func([$entity, 'get' . ucfirst($associationMappingKey)]); $changed = false; foreach ($associated as $associatedEntity) { if (!$newAssociated->contains($associatedEntity)) { $changed = true; $em->remove($associatedEntity); } } if ($changed) { $em->flush(); } // } } // dump($classMetaData); // die(); }
/** * @param EventArgs|LifecycleEventArgs $args */ public function postPersist(LifecycleEventArgs $args) { $entity = $args->getObject(); $em = $args->getObjectManager(); if ($entity instanceof Comment) { if ($entity->getArticleId() != null) { if ($entity->getUserId()->getId() != $entity->getArticleId()->getUserId()->getId()) { $n = new Notification(); $n->setUser($entity->getUserId())->setArticle($entity->getArticleId()); $em->persist($n); $em->flush(); } } else { if ($entity->getVideoId() != null) { if ($entity->getUserId()->getId() != $entity->getVideoId()->getUserId()->getId()) { $n = new Notification(); $n->setUser($entity->getUserId())->setVideo($entity->getVideoId()); $em->persist($n); $em->flush(); } } else { if ($entity->getLinkId() != null) { if ($entity->getUserId()->getId() != $entity->getLinkId()->getUserId()->getId()) { $n = new Notification(); $n->setUser($entity->getUserId())->setLink($entity->getLinkId()); $em->persist($n); $em->flush(); } } } } } }
/** * Pre update * PreUpdate event needs to recompute change set * * @param LifecycleEventArgs $args */ public function preUpdate(LifecycleEventArgs $args) { $product = $args->getObject(); if ($product instanceof ProductInterface) { $dm = $args->getObjectManager(); $this->convertMetricValues($dm, $product); } }
/** * Post-truncate * * @param \Doctrine\Common\Persistence\Event\LifecyleEventArgs $args */ public function postTruncate(LifecycleEventArgs $args) { $connection = $args->getObjectManager()->getConnection(); $platform = $connection->getDatabasePlatform(); if ($platform instanceof MySqlPlatform) { $connection->exec('SET foreign_key_checks = 1;'); } }
/** * Pre update listener based on doctrine common * * @param LifecycleEventArgs $args */ public function preUpdate(LifecycleEventArgs $args) { $object = $args->getObject(); if ($object instanceof UserInterface) { $this->updateUserFields($object); $this->recomputeChangeSet($args->getObjectManager(), $object); } }
/** * @param LifecycleEventArgs $lifecycleEventArgs */ private function updateSlug(LifecycleEventArgs $lifecycleEventArgs) { $object = $lifecycleEventArgs->getObject(); $productTranslationRepository = $lifecycleEventArgs->getObjectManager()->getRepository(get_class($object)); $slug = $this->slugify($object->getName(), $productTranslationRepository); $object->setSlug($slug); $this->generatedSlugs[] = $slug; }
public function prePersist(LifecycleEventArgs $args) { $entity = $args->getObject(); if ($entity instanceof PaymentInterface) { $generator = new UuidGenerator(); $token = $generator->generate($args->getObjectManager(), null); $entity->setToken($token); } }
/** * Pre update * PreUpdate event needs to recompute change set * * @param LifecycleEventArgs $args */ public function preUpdate(LifecycleEventArgs $args) { $object = $args->getObject(); if ($object instanceof MetricInterface && $object->getUnit()) { $this->createMetricBaseValues($object); $class = new ClassMetadata($object); $args->getObjectManager()->getUnitOfWork()->recomputeSingleDocumentChangeSet($class, $object); } }
public function postLoad(LifecycleEventArgs $args) { $object = $args->getObject(); if ($object instanceof ComponentInterface) { $resolver = new Resolver($args->getObjectManager()); $resolverComponent = new ResolverComponentModelIdentifier($object->getModel(), $object->getIdentifier()); $object->setData($resolver->resolve($resolverComponent)); } }
/** * @ORM\PreRemove * @param LifecycleEventArgs $args * @throws \Exception */ public function deleteChildren(LifecycleEventArgs $args) { $objectManager = $args->getObjectManager(); $entityType = $objectManager->getRepository('Comment\\Entity\\EntityType')->findOneByAlias('comment'); $commentRepository = $objectManager->getRepository('Comment\\Entity\\Comment'); $comments = $commentRepository->findBy(array('entityType' => $entityType, 'entityId' => $this->getId())); foreach ($comments as $comment) { $objectManager->remove($comment); } }
public function prePersist(LifecycleEventArgs $args) { /** @var User $entity */ $entity = $args->getObject(); /** @var EntityManager $em */ $em = $args->getObjectManager(); if ($entity instanceof User && $entity->getTeamId() && !$entity->getTeam()) { /** @var Team $team */ $team = $em->getRepository('Patgod85TeamBundle:Team')->find($entity->getTeamId()); $entity->setTeam($team); } }
/** * Set the timestamps. If created is NULL then set it. Always * set the changed field. * * @param LifecycleEventArgs $event */ private function handleTimestamp(LifecycleEventArgs $event) { $entity = $event->getObject(); if (!$entity instanceof TimestampableInterface) { return; } $meta = $event->getObjectManager()->getClassMetadata(get_class($entity)); $created = $meta->getFieldValue($entity, self::CREATED_FIELD); if (null === $created) { $meta->setFieldValue($entity, self::CREATED_FIELD, new \DateTime()); } $meta->setFieldValue($entity, self::CHANGED_FIELD, new \DateTime()); }
protected function clearCache(LifecycleEventArgs $args) { $entity = $args->getObject(); $classes = $this->classes; foreach ($classes as $class) { if ($entity instanceof $class) { $this->app['monolog']->debug('clear result cache: ' . $class); $cache = $args->getObjectManager()->getConfiguration()->getResultCacheImpl(); $cache->deleteAll(); break; } } }
/** * Before update * * @param LifecycleEventArgs $args */ public function preUpdate(LifecycleEventArgs $args) { $object = $args->getObject(); if ($object instanceof ProductValueInterface) { $product = $object->getEntity(); if ($product !== null) { $this->updateProductFields($args->getObjectManager(), $product, array('updated')); } } if ($object instanceof TimestampableInterface) { $object->setUpdated(new \DateTime('now', new \DateTimeZone('UTC'))); } }
/** * @param LifecycleEventArgs $args * @return mixed|void */ public function postPersist(LifecycleEventArgs $args) { $entity = $args->getObject(); /** @var EntityManager $em */ $em = $args->getObjectManager(); if ($entity instanceof PageInterface) { if ($contentRoute = $entity->getContentRoute()) { $contentRoute->setObjectId($entity->getId()); $contentRoute->setPath(PageHelper::getPageRoutePath($entity->getPath())); $em->persist($contentRoute); $em->getUnitOfWork()->computeChangeSet($em->getClassMetadata(get_class($contentRoute)), $contentRoute); } } }
/** * Cette methode sera appelé depuis mon services.yml * ET reçoie en argument mon evenement Doctrine 2 * @param LifecycleEventArgs $args */ public function postUpdate(LifecycleEventArgs $args) { // Je récupère mon objet après modification (update) $entity = $args->getObject(); $em = $args->getObjectManager(); // récupérer l'Entité Manager // Si mon objet est un objet de mon entité Product if ($entity instanceof Product) { // quand la quantité sera égal à 1 if ($entity->getQuantity() < 5) { $this->notif->notify('Attention, le produit' . $entity->getTitle() . ' a une quantité inférieur à 5'); } } }
public function preRemove(LifecycleEventArgs $args) { if (!$args->getObject() instanceof Payment) { return; } $entity = $args->getObject(); $entityManager = $args->getObjectManager(); $products = $entityManager->getRepository("CvaGestionMembreBundle:Produit")->findBy(['hasWaitingList' => true]); if ($entity instanceof Payment) { if (in_array($entity->getProduct(), $products)) { $this->containerInterface->get("bde.wei.registration_management")->removeFromWaitingList($entity->getStudent(), $entity->getProduct()); } } }
/** * @param LifecycleEventArgs $args * @return mixed|void */ public function preUpdate(LifecycleEventArgs $args) { $entity = $args->getObject(); $em = $args->getObjectManager(); $uow = $em->getUnitOfWork(); if ($entity instanceof ContentRoute) { $template = $this->templates[$entity->getTemplateName()]; $changeset = $uow->getEntityChangeSet($entity); if (isset($changeset['templateName']) && $args->hasChangedField('templateName')) { $entity->setTemplate($template['template']); $entity->setController($template['controller']); $uow->recomputeSingleEntityChangeSet($em->getClassMetadata("NetworkingInitCmsBundle:ContentRoute"), $entity); } } }
function it_converts_metric_data_before_updating(LifecycleEventArgs $args, MetricInterface $metric, MeasureManager $manager, MeasureConverter $converter, DocumentManager $dm, UnitOfWork $uow) { $args->getObject()->willReturn($metric); $args->getObjectManager()->willReturn($dm); $dm->getUnitOfWork()->willReturn($uow); $uow->recomputeSingleDocumentChangeSet(Argument::type('Doctrine\\Common\\Persistence\\Mapping\\ClassMetadata'), $metric)->shouldBeCalled(); $metric->getUnit()->willReturn('cm'); $metric->getFamily()->willReturn('distance'); $metric->getData()->willReturn(100); $manager->getStandardUnitForFamily('distance')->willReturn('m'); $converter->setFamily('distance')->shouldBeCalled()->willReturn($converter); $converter->convertBaseToStandard('cm', 100)->willReturn(1); $metric->setBaseData(1)->shouldBeCalled()->willReturn($metric); $metric->setBaseUnit('m')->shouldBeCalled(); $this->preUpdate($args); }
/** * */ protected function triggerStorage(LifecycleEventArgs $args) { $entity = $args->getObject(); if (!$entity instanceof StorageInterface) { return; } $manager = $args->getObjectManager(); $metadata = $manager->getClassMetadata(get_class($entity)); foreach ($metadata->getFieldNames() as $field) { $type = $metadata->getTypeOfField($field); if ($type == 'file') { $custom_store_method = 'store' . ucfirst($field); if (method_exists($entity, $custom_store_method)) { $entity->{$custom_store_method}($this); } else { $entity->storeField($this, $field); } } } }
public function prePersist(LifecycleEventArgs $args) { $document = $args->getObject(); $metadataFactory = $args->getObjectManager()->getMetadataFactory(); $odmMetadata = $metadataFactory->getMetadataFor(ClassUtils::getRealClass(get_class($document))); if (null === ($ctMetadata = $this->metadataFactory->getMetadataForClass($odmMetadata->getName()))) { return; } foreach ($odmMetadata->childrenMappings as $childrenField) { // if the children field is not managed by the CT component, // continue if (!isset($ctMetadata->propertyMetadata[$childrenField])) { continue; } $childCtMetadata = $ctMetadata->propertyMetadata[$childrenField]; $children = $odmMetadata->getFieldValue($document, $childrenField); if (!$children) { continue; } $this->stack[] = ['children' => $children, 'ct_metadata' => $childCtMetadata, 'field' => $childrenField]; } }
/** * Delete document on objects after being remove * * @param LifecycleEventArgs $args * @return void */ public function preRemove(LifecycleEventArgs $args) { $entity = $args->getEntity(); $entityManager = $args->getObjectManager(); $meta = $entityManager->getClassMetadata(get_class($entity)); foreach ($meta->fieldMappings as $field => $options) { $reflProperty = new ReflectionProperty($meta->getName(), $field); $annotation = $this->getAnnotationReader()->getPropertyAnnotation($reflProperty, '\\Keratine\\Doctrine\\Mapping\\Annotation\\Thumbnail'); if ($annotation) { $accessor = PropertyAccess::createPropertyAccessor(); $sourcePath = $accessor->getValue($entity, $annotation->path); if (!$sourcePath) { return; } foreach ($annotation->sizes as $name => $size) { $destinationPath = dirname($sourcePath) . '/' . $name . '/' . basename($sourcePath); if (file_exists($destinationPath)) { unlink($destinationPath); } } } } }
public function postUpdate(LifecycleEventArgs $args) { $object = $args->getObject(); if ($object instanceof CategoryInterface && $this->preCategory && !$object->isRoot()) { $em = $args->getObjectManager(); if ($object->getParent()->getId() == $this->preCategory->getParent()->getId()) { $qb = $em->createQueryBuilder()->update(get_class($object), 'category'); if ($object->getWeight() > $this->preCategory->getWeight()) { $qb->set('category.weight', 'category.weight - 1')->where('category.id != :id and category.parent = :parent and category.weight >= :min and category.weight <= :max')->setParameter('id', $object->getId())->setParameter('parent', $object->getParent())->setParameter('min', $this->preCategory->getWeight() + 1)->setParameter('max', $object->getWeight()); $qb->getQuery()->execute(); } elseif ($object->getWeight() < $this->preCategory->getWeight()) { $qb->set('category.weight', 'category.weight + 1')->where('category.id != :id and category.parent = :parent and category.weight >= :min and category.weight <= :max')->setParameter('id', $object->getId())->setParameter('parent', $object->getParent())->setParameter('min', $object->getWeight())->setParameter('max', $this->preCategory->getWeight() - 1); $qb->getQuery()->execute(); } } else { //pre $qb = $em->createQueryBuilder()->update(get_class($object), 'category')->set('category.weight', 'category.weight - 1')->where('category.id != :id and category.parent = :parent and category.weight > :weight')->setParameter('id', $object->getId())->setParameter('parent', $this->preCategory->getParent())->setParameter('weight', $this->preCategory->getWeight()); $qb->getQuery()->execute(); //new $qb = $em->createQueryBuilder()->update(get_class($object), 'category')->set('category.weight', 'category.weight + 1')->where('category.id != :id and category.parent = :parent and category.weight >= :weight')->setParameter('id', $object->getId())->setParameter('parent', $object->getParent())->setParameter('weight', $object->getWeight())->setParameter('id', $object->getId()); $qb->getQuery()->execute(); } } }
function it_does_not_generate_duplicated_slugs_based_on_database_when_updating(LifecycleEventArgs $lifecycleEventArgs, ObjectManager $objectManager, ProductTranslationInterface $productTranslation, ObjectRepository $productTranslationRepository) { $lifecycleEventArgs->getObjectManager()->willReturn($objectManager); $objectManager->getRepository(get_class($productTranslation->getWrappedObject()))->willReturn($productTranslationRepository); $lifecycleEventArgs->getObject()->willReturn($productTranslation); $productTranslation->getName()->willReturn('Banana'); $productTranslationRepository->findOneBy(['slug' => 'banana'])->willReturn(new \stdClass()); $productTranslationRepository->findOneBy(['slug' => 'banana-1'])->willReturn(null); $productTranslation->setSlug('banana-1')->shouldBeCalled(); $this->preUpdate($lifecycleEventArgs); }
/** * Update locale after persisting a route. * * @param LifecycleEventArgs $args */ public function postPersist(LifecycleEventArgs $args) { $doc = $args->getObject(); if (!$doc instanceof Route) { return; } $this->updateLocale($doc, $doc->getId(), $args->getObjectManager()); }
/** * * @param object $entity * @return FileUpload|NULL */ private function getFileUploadAnnotation(LifecycleEventArgs $args) { $entity = $args->getEntity(); $reflectionClass = $args->getObjectManager()->getClassMetadata(get_class($entity))->getReflectionClass(); return $this->reader->getClassAnnotation($reflectionClass, self::ANNOTATION); }
/** * @param Alias $alias * @param LifecycleEventArgs $args * * @return bool */ private function validateUnique(Alias $alias, LifecycleEventArgs $args) { return (bool) $args->getObjectManager()->getRepository('ClasticAliasBundle:Alias')->findBy(array('alias' => $alias->getAlias())); }
function it_does_not_apply_on_non_version_object(LifecycleEventArgs $args, \stdClass $object) { $args->getObject()->willReturn($object); $args->getObjectManager()->shouldNotBeCalled(); $this->prePersist($args); }