Logs entity changeset

@param LifecycleEventArgs $eventArgs
974758
public function logChangeSet(LifecycleEventArgs $eventArgs)
    {
        $em            = $eventArgs->getEntityManager();
        $uow           = $em->getUnitOfWork();
        $entity        = $eventArgs->getEntity();
        $classMetadata = $em->getClassMetadata(get_class($entity));

        if ($this->isEntitySupported($classMetadata->reflClass)) {
            $uow->computeChangeSet($classMetadata, $entity);
            $changeSet = $uow->getEntityChangeSet($entity);

            $message = $entity->getUpdateLogMessage($changeSet);
            $loggerCallable = $this->loggerCallable;
            $loggerCallable($message);
        }
    }
179620
public function postLoad(LifecycleEventArgs $args)
    {
        $entity = $args->getEntity();

        if (!$entity instanceof TranslatableInterface) {
            return;
        }

        $entity->initializeTranslations();
        $entity->setCurrentLocale($this->localeProvider->getCurrentLocale());
        $entity->setFallbackLocale($this->localeProvider->getFallbackLocale());
    }
1161541
public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs)
    {
        /** @var ClassMetadata $classMetadata */
        $classMetadata = $eventArgs->getClassMetadata();

        if (null === $classMetadata->getReflectionClass() ||
            false == $this->isModular($classMetadata) ||
            'Harmony\Component\ModularRouting\StaticModule' == $this->getModuleClass() ||
            $classMetadata->hasField('module') || $classMetadata->hasAssociation('module')) {

            return;
        }

        $classMetadata->mapManyToOne([
            'targetEntity' => $this->getModuleClass(),
            'fieldName'    => 'module',
            'joinColumns'  => [
                [
                    'name'                 => 'module_id',
                    'referencedColumnName' => 'id',
                ],
            ],
        ]);
    }
99435
public function loadEntitiesAtLoad(LifecycleEventArgs $event)
    {
        if ($this->entityValue && $this->entityValue['entityClass']) {
            $em = $event->getEntityManager();
            if (is_array($this->entityValue) && is_array($this->entityValue['entityId'])) {
                // multiple
                $entityData = $em->getRepository($this->entityValue['entityClass'])->findById($this->entityValue['entityId']); // in this case, $this->entityValue['entityId'] contains an array of entity IDs
            } else {
                // single
                $entityData = $em->getRepository($this->entityValue['entityClass'])->findOneById($this->entityValue['entityId']);
            }

            $this->entityData = $entityData;
        } else {
            $this->entityData = null;
        }
    }
1231522
public function onFlush(OnFlushEventArgs $eventArgs)
	{
		$em = $eventArgs->getEntityManager();
		$uow = $em->getUnitOfWork();

		foreach ($uow->getScheduledEntityUpdates() as $entity) {
			if ($entity instanceof TimestampableInterface) {
				$entity->setModificationTime();
				$className = get_class($entity);
				$class = $em->getClassMetadata($className);
				$uow->recomputeSingleEntityChangeSet($class, $entity);
			}
		}
	}
235075
public function postLoad(LifecycleEventArgs $args)
    {
        $this
            ->container
            ->get('elcodi.entity_translator')
            ->translate(
                $args->getEntity(),
                $this->locale->getIso()
            );
    }
1284218
public function onClear($eventArgs)
    {
        parent::onClear($eventArgs);

        $aliases             = array_keys($this->identifierMap);
        $this->identifierMap = array();

        foreach ($aliases as $alias) {
            $this->identifierMap[$alias] = array();
        }
    }
931923
public function postPersist(LifecycleEventArgs $args)
    {
        $entity = $args->getObject();
        if ($entity instanceof PackageVersionEntity) {
            $this->refreshPackageLatestVersion($args->getObjectManager(), $entity->getPackage());
        }
    }
817252
public function postRemoveEntity(LifecycleEventArgs $eventArgs)
    {
        $file = $eventArgs->getDocument();
        if (!$file instanceof Attachment) {
            return;
        }
        
        $fileId = new \MongoId($file->getId());
        $dm = $eventArgs->getDocumentManager();
        $dm->createQueryBuilder('Cv\Entity\Cv')
           ->update()->multiple(true)
           ->field('attachments')->equals($fileId)->pull($fileId)
           ->getQuery()->execute();
    }
1018746
public function postFlush(PostFlushEventArgs $eventArgs)
    {
        $em = $eventArgs->getEntityManager();
        $quoteStrategy = $em->getConfiguration()->getQuoteStrategy();
        $uow = $em->getUnitOfWork();

        foreach ($this->extraUpdates as $entity) {
            $className = \get_class($entity);
            $meta = $em->getClassMetadata($className);

            $persister = $uow->getEntityPersister($className);
            $updateData = $this->prepareUpdateData($persister, $entity);

            if (!isset($updateData[$meta->table['name']]) || !$updateData[$meta->table['name']]) {
                continue;
            }

            /** @var array $updateDataColumns */
            $updateDataColumns = $updateData[$meta->table['name']];

            foreach ($updateDataColumns as $column => $value) {
                $field = $meta->getFieldName($column);
                $fieldName = $meta->getFieldForColumn($column);
                $placeholder = '?';
                if ($meta->hasField($fieldName)) {
                    $field = $quoteStrategy->getColumnName($field, $meta, $this->platform);
                    $fieldType = $meta->getTypeOfField($field);
                    if (null !== $fieldType) {
                        $type = Type::getType($fieldType);
                        if ($type->canRequireSQLConversion()) {
                            $placeholder = $type->convertToDatabaseValueSQL('?', $this->platform);
                        }
                    }
                }

                $sql = 'UPDATE ' . $this->config->getTableName($meta) . ' ' .
                    'SET ' . $field . ' = ' . $placeholder . ' ' .
                    'WHERE ' . $this->config->getRevisionFieldName() . ' = ? ';

                $isIgnore = false;

                foreach ($this->config->getGlobalIgnoreColumns() as $columnName) {
                    if ($field === $columnName) {
                        $isIgnore = true;
                    }
                }

                if (isset($this->auditedEntitiesIgnoreColumns[$className])) {
                    /** @var array $auditedEntitiesIgnoreColumns */
                    $auditedEntitiesIgnoreColumns = $this->auditedEntitiesIgnoreColumns[$className];

                    foreach ($auditedEntitiesIgnoreColumns as $columnName) {
                        if ($field === $columnName) {
                            $isIgnore = true;
                        }
                    }
                }

                if ($isIgnore) {
                    continue;
                }

                $params = [$value, $this->getRevisionId()];

                $types = [];

                if (\in_array($column, $meta->getColumnNames(), true)) {
                    $types[] = $meta->getTypeOfField($fieldName);
                } else {
                    //try to find column in association mappings
                    $type = null;

                    foreach ($meta->associationMappings as $mapping) {
                        if (isset($mapping['joinColumns'])) {
                            /** @var array $joinColumns */
                            $joinColumns = $mapping['joinColumns'];

                            /** @var array $definition */
                            foreach ($joinColumns as $definition) {
                                if ($definition['name'] === $column) {
                                    $targetTable = $em->getClassMetadata($mapping['targetEntity']);
                                    $type = $targetTable->getTypeOfField($targetTable->getFieldName($definition['referencedColumnName']));
                                }
                            }
                        }
                    }

                    if (null === $type) {
                        throw new \RuntimeException(
                            sprintf('Could not resolve database type for column "%s" during extra updates', $column)
                        );
                    }

                    $types[] = $type;
                }

                $types[] = $this->config->getRevisionIdFieldType();

                foreach ($meta->identifier as $idField) {
                    if (isset($meta->fieldMappings[$idField])) {
                        $columnName = $meta->fieldMappings[$idField]['columnName'];
                        $types[] = $meta->fieldMappings[$idField]['type'];

                        $params[] = $meta->reflFields[$idField]->getValue($entity);

                        $sql .= 'AND ' . $columnName . ' = ?';
                    } elseif (isset($meta->associationMappings[$idField])) {
                        $columnName = $meta->associationMappings[$idField]['joinColumns'][0];
                        if (\is_array($columnName)) {
                            if (isset($columnName['name'])) {
                                $columnName = $columnName['name'];
                            } else {
                                // Not much we can do to recover this - we need a column name...
                                throw new MappingException('Column name not set within meta');
                            }
                        }
                        $types[] = $meta->associationMappings[$idField]['type'];

                        $params[] = $meta->reflFields[$idField]->getValue($entity);

                        $sql .= 'AND ' . $columnName . ' = ?';
                    }
                }

                $this->em->getConnection()->executeQuery($sql, $params, $types);
            }
        }
    }
554477
private function createNestedSetNode(LifecycleEventArgs $args)
	{
		$entity = $args->getEntity();

		if ($entity instanceof EntityNodeInterface) {
			// Read entity data from the event arguments
			$em = $args->getEntityManager();
			
			// Find the repository
			$entityName = $entity->getNestedSetRepositoryClassName();
			$repository = $em->getRepository($entityName);
			
			// In case of refresh it already exists..
			$doctrineNode = $entity->getNestedSetNode();
			
			// .. create new if doesn't
			if ($doctrineNode === null) {
				// Initialize the doctrine nested set node
				$doctrineNode = new DoctrineNode($repository);
				$entity->setNestedSetNode($doctrineNode);
			}

			$doctrineNode->belongsTo($entity);
		}
	}
1120054
public function postLoad(EventArgs $eventArgs)
    {
        if (method_exists($eventArgs, 'getEntity')) {
            $this->_triggerEvent(Events::postLoad, $eventArgs->getEntity(), $eventArgs);
        }
    }
786841
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);
            }
        }
    }
842467
public function onFlush(OnFlushEventArgs $eventArgs)
    {
        $this->em = $eventArgs->getEntityManager();
        $this->quoteStrategy = $this->em->getConfiguration()->getQuoteStrategy();
        $this->conn = $this->em->getConnection();
        $this->uow = $this->em->getUnitOfWork();
        $this->platform = $this->conn->getDatabasePlatform();
        $this->revisionId = null; // reset revision

        $processedEntities = [];

        foreach ($this->uow->getScheduledEntityDeletions() as $entity) {
            //doctrine is fine deleting elements multiple times. We are not.
            $hash = $this->getHash($entity);

            if (\in_array($hash, $processedEntities, true)) {
                continue;
            }

            $processedEntities[] = $hash;

            $class = $this->em->getClassMetadata(\get_class($entity));
            if (!$this->metadataFactory->isAudited($class->name)) {
                continue;
            }

            $entityData = array_merge($this->getOriginalEntityData($entity), $this->uow->getEntityIdentifier($entity));
            $this->saveRevisionEntityData($class, $entityData, 'DEL');
        }

        foreach ($this->uow->getScheduledEntityInsertions() as $entity) {
            if (!$this->metadataFactory->isAudited(\get_class($entity))) {
                continue;
            }

            $this->extraUpdates[spl_object_hash($entity)] = $entity;
        }

        foreach ($this->uow->getScheduledEntityUpdates() as $entity) {
            if (!$this->metadataFactory->isAudited(\get_class($entity))) {
                continue;
            }

            $this->extraUpdates[spl_object_hash($entity)] = $entity;
        }
    }
675225
public function onFlush(OnFlushEventArgs $eventArgs)
    {
        $this->eventQueue->setOpened(true);

        $uow = $eventArgs->getEntityManager()->getUnitOfWork();

        foreach ($uow->getScheduledEntityInsertions() as $entity) {
            if ($entity instanceof ResourceInterface) {
                $this->eventQueue->scheduleInsert($entity);
            }
        }

        foreach ($uow->getScheduledEntityUpdates() as $entity) {
            if ($entity instanceof ResourceInterface) {
                $this->eventQueue->scheduleUpdate($entity);
            }
        }

        // TODO move collections before update ?
        foreach ($uow->getScheduledCollectionDeletions() as $col) {
            foreach ($col as $c) {
                foreach ($c as $entity) {
                    if ($entity instanceof ResourceInterface) {
                        $this->eventQueue->scheduleDelete($entity);
                    }
                }
            }
        }
        foreach ($uow->getScheduledCollectionUpdates() as $col) {
            foreach ($col as $c) {
                foreach ($c as $entity) {
                    if ($entity instanceof ResourceInterface) {
                        if ($entity->getId()) {
                            $this->eventQueue->scheduleUpdate($entity);
                        } else {
                            $this->eventQueue->scheduleInsert($entity);
                        }
                    }
                }
            }
        }

        foreach ($uow->getScheduledEntityDeletions() as $entity) {
            if ($entity instanceof ResourceInterface) {
                $this->eventQueue->scheduleDelete($entity);
            }
        }

        $this->eventQueue->flush();

        $this->eventQueue->setOpened(false);
    }
78693
public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs)
    {
        /** @var ClassMetadata $metadata */
        $metadata = $eventArgs->getClassMetadata();

        $reflectionClass = $metadata->getReflectionClass();

        if (!$reflectionClass || !$this->hasTrait($reflectionClass, 'Blast\BaseEntitiesBundle\Entity\Traits\Timestampable')) {
            return;
        } // return if current entity doesn't use Timestampable trait

        // Check if parents already have the Timestampable trait
        foreach ($metadata->parentClasses as $parent) {
            if ($this->classAnalyzer->hasTrait($parent, 'Blast\BaseEntitiesBundle\Entity\Traits\Timestampable')) {
                return;
            }
        }

        $this->logger->debug(
            '[TimestampableListener] Entering TimestampableListener for « loadClassMetadata » event'
        );

        // setting default mapping configuration for Timestampable

        // createdAt
        $metadata->mapField([
            'fieldName' => 'createdAt',
            'type'      => 'datetime',
        ]);

        // updatedAt
        $metadata->mapField([
            'fieldName' => 'updatedAt',
            'type'      => 'datetime',
        ]);

        $this->logger->debug(
            '[TimestampableListener] Added Timestampable mapping metadata to Entity',
            ['class' => $metadata->getName()]
        );
    }
95597
public function preFlush(EventArgs $eventArgs)
    {
        $em = $eventArgs->getEntityManager();
        $uow = $em->getUnitOfWork();

        foreach ($uow->getScheduledEntityInsertions() as $entity) {
            $this->_triggerEvent(Events::preFlush, $entity, $eventArgs);
        }

        foreach ($uow->getScheduledEntityUpdates() as $entity) {
            $this->_triggerEvent(Events::preFlush, $entity, $eventArgs);
        }

        foreach ($uow->getScheduledEntityDeletions() as $entity) {
            $this->_triggerEvent(Events::preFlush, $entity, $eventArgs);
        }
    }