public function postGenerateSchemaTable(GenerateSchemaTableEventArgs $eventArgs) { $cm = $eventArgs->getClassMetadata(); if (!$this->metadataFactory->isAudited($cm->name)) { $audited = false; if ($cm->isInheritanceTypeJoined() && $cm->rootEntityName == $cm->name) { foreach ($cm->subClasses as $subClass) { if ($this->metadataFactory->isAudited($subClass)) { $audited = true; } } } if (!$audited) { return; } } $schema = $eventArgs->getSchema(); $entityTable = $eventArgs->getClassTable(); $revisionTable = $schema->createTable($this->config->getTablePrefix() . $entityTable->getName() . $this->config->getTableSuffix()); foreach ($entityTable->getColumns() as $column) { /* @var $column Column */ $revisionTable->addColumn($column->getName(), $column->getType()->getName(), array_merge($column->toArray(), array('notnull' => false, 'autoincrement' => false))); } $revisionTable->addColumn($this->config->getRevisionFieldName(), $this->config->getRevisionIdFieldType()); $revisionTable->addColumn($this->config->getRevisionTypeFieldName(), 'string', array('length' => 4)); if (!in_array($cm->inheritanceType, array(ClassMetadataInfo::INHERITANCE_TYPE_NONE, ClassMetadataInfo::INHERITANCE_TYPE_JOINED, ClassMetadataInfo::INHERITANCE_TYPE_SINGLE_TABLE))) { throw new \Exception(sprintf('Inheritance type "%s" is not yet supported', $cm->inheritanceType)); } $pkColumns = $entityTable->getPrimaryKey()->getColumns(); $pkColumns[] = $this->config->getRevisionFieldName(); $revisionTable->setPrimaryKey($pkColumns); }
public function postGenerateSchemaTable(GenerateSchemaTableEventArgs $eventArgs) { $cm = $eventArgs->getClassMetadata(); if ($this->metadataFactory->isAudited($cm->name)) { $schema = $eventArgs->getSchema(); $entityTable = $eventArgs->getClassTable(); $revisionTable = $schema->createTable($this->config->getTablePrefix() . $entityTable->getName() . $this->config->getTableSuffix()); foreach ($entityTable->getColumns() as $column) { /* @var $column Column */ $revisionTable->addColumn($column->getName(), $column->getType()->getName(), array_merge($column->toArray(), array('notnull' => false, 'autoincrement' => false))); } $revisionTable->addColumn($this->config->getRevisionFieldName(), $this->config->getRevisionIdFieldType()); $revisionTable->addColumn($this->config->getRevisionTypeFieldName(), 'string', array('length' => 4)); $pkColumns = $entityTable->getPrimaryKey()->getColumns(); $pkColumns[] = $this->config->getRevisionFieldName(); $revisionTable->setPrimaryKey($pkColumns); } }
public function getEntityHistory($className, $id) { if (!$this->metadataFactory->isAudited($className)) { throw new NotAuditedException($className); } /** @var ClassMetadataInfo|ClassMetadata $class */ $class = $this->em->getClassMetadata($className); $tableName = $this->config->getTableName($class); if (!is_array($id)) { $id = array($class->identifier[0] => $id); } $whereId = array(); foreach ($class->identifier as $idField) { if (isset($class->fieldMappings[$idField])) { $columnName = $class->fieldMappings[$idField]['columnName']; } else { if (isset($class->associationMappings[$idField])) { $columnName = $class->associationMappings[$idField]['joinColumns'][0]; } else { continue; } } $whereId[] = "{$columnName} = ?"; } $whereSQL = implode(' AND ', $whereId); $columnList = array($this->config->getRevisionFieldName()); $columnMap = array(); foreach ($class->fieldNames as $columnName => $field) { $type = Type::getType($class->fieldMappings[$field]['type']); $columnList[] = $type->convertToPHPValueSQL($class->getQuotedColumnName($field, $this->platform), $this->platform) . ' AS ' . $this->platform->quoteSingleIdentifier($field); $columnMap[$field] = $this->platform->getSQLResultCasing($columnName); } foreach ($class->associationMappings as $assoc) { if (($assoc['type'] & ClassMetadata::TO_ONE) == 0 || !$assoc['isOwningSide']) { continue; } foreach ($assoc['targetToSourceKeyColumns'] as $sourceCol) { $columnList[] = $sourceCol; $columnMap[$sourceCol] = $this->platform->getSQLResultCasing($sourceCol); } } $values = array_values($id); $query = "SELECT " . implode(', ', $columnList) . " FROM " . $tableName . " e WHERE " . $whereSQL . " ORDER BY e.rev DESC"; $stmt = $this->em->getConnection()->executeQuery($query, $values); $result = array(); while ($row = $stmt->fetch(Query::HYDRATE_ARRAY)) { $rev = $row[$this->config->getRevisionFieldName()]; unset($row[$this->config->getRevisionFieldName()]); $result[] = $this->createEntity($class->name, $row, $rev); } return $result; }
public function onFlush(OnFlushEventArgs $eventArgs) { $this->em = $eventArgs->getEntityManager(); $this->conn = $this->em->getConnection(); $this->uow = $this->em->getUnitOfWork(); $this->platform = $this->conn->getDatabasePlatform(); $this->revisionId = null; // reset revision foreach ($this->uow->getScheduledEntityDeletions() as $entity) { $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'); } }
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 = array(); 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)) { 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; } }