/** * @param GenerateSchemaTableEventArgs $eventArgs * * @throws \Exception */ public function postGenerateSchemaTable(GenerateSchemaTableEventArgs $eventArgs) { $cm = $eventArgs->getClassMetadata(); if (!$this->annotationReader->isRevised($cm->name)) { $revised = false; if ($cm->isInheritanceTypeJoined() && $cm->rootEntityName == $cm->name) { foreach ($cm->subClasses as $subClass) { if ($this->annotationReader->isRevised($subClass)) { $revised = true; } } } if (!$revised) { return; } } if (!in_array($cm->inheritanceType, array(ClassMetadataInfo::INHERITANCE_TYPE_NONE, ClassMetadataInfo::INHERITANCE_TYPE_SINGLE_TABLE))) { throw new \Exception(sprintf('Inheritance type "%s" is not yet supported', $cm->inheritanceType)); } $schema = $eventArgs->getSchema(); $entityTable = $eventArgs->getClassTable(); $revisionTable = $schema->createTable($entityTable->getName() . '_revisions'); $revisionTable->addColumn('revision_id', 'integer'); $revisionTable->addColumn('rev_type', 'string', array('length' => 4)); foreach ($entityTable->getColumns() as $column) { if (!in_array($column->getName(), $cm->identifier) && !$this->isColumnRevised($cm, $column->getName())) { continue; } /* @var Column $column */ $revisionTable->addColumn($column->getName(), $column->getType()->getName(), array_merge($column->toArray(), array('notnull' => false, 'autoincrement' => false))); } $pkColumns = $entityTable->getPrimaryKey()->getColumns(); $pkColumns[] = 'revision_id'; $revisionTable->setPrimaryKey($pkColumns); }
public function onFlush(OnFlushEventArgs $eventArgs) { if (!$this->active) { return; } // Clear updateData $this->updateData = $this->extraUpdates = array(); $this->em = $eventArgs->getEntityManager(); $this->conn = $this->em->getConnection(); $this->uow = $this->em->getUnitOfWork(); $this->platform = $this->conn->getDatabasePlatform(); $this->revisionId = null; // reset revision $this->draft = false; $processedEntities = array(); foreach ($this->uow->getScheduledEntityDeletions() as $entity) { if (!$this->annotationReader->isRevised(get_class($entity), true)) { continue; } //doctrine is fine deleting elements multiple times. We are not. $hash = $this->getHash($entity); if (in_array($hash, $processedEntities)) { continue; } $processedEntities[] = $hash; $this->extraUpdates[spl_object_hash($entity)] = $entity; $persister = $this->uow->getEntityPersister(get_class($entity)); $this->updateData[spl_object_hash($entity)] = $this->prepareUpdateData($persister, $entity); $entityData = array_merge($this->getOriginalEntityData($entity), $this->uow->getEntityIdentifier($entity)); $this->saveRevisionEntityData($this->em->getClassMetadata(get_class($entity)), $entityData, 'DEL'); if ($this->annotationReader->isDraft($entity) && $entity->isDraft()) { $this->resetRevisedData($entity); $this->setRevisionInfo($entity); $persister = $this->uow->getEntityPersister(get_class($entity)); $this->updateData[spl_object_hash($entity)] = $this->prepareUpdateData($persister, $entity); $fieldName = 'deletedAt'; $reflProp = new \ReflectionProperty($entity, $fieldName); $reflProp->setAccessible(true); $oldValue = $reflProp->getValue($entity); $reflProp->setValue($entity, null); $this->uow->scheduleExtraUpdate($entity, array($fieldName => array($oldValue, null))); } if (isset($this->softDeletes[spl_object_hash($entity)])) { $this->em->persist($entity); } } foreach ($this->uow->getScheduledEntityInsertions() as $entity) { if (!$this->annotationReader->isRevised(get_class($entity), true)) { continue; } $this->setRevisionInfo($entity); $this->extraUpdates[spl_object_hash($entity)] = $entity; $persister = $this->uow->getEntityPersister(get_class($entity)); $this->updateData[spl_object_hash($entity)] = $this->prepareUpdateData($persister, $entity); if ($this->annotationReader->isDraft($entity) && $entity->isDraft()) { $this->insertDrafts[spl_object_hash($entity)] = $entity; $this->resetRevisedData($entity); $this->uow->recomputeSingleEntityChangeSet($this->em->getClassMetadata(get_class($entity)), $entity); } } foreach ($this->uow->getScheduledEntityUpdates() as $entity) { if (!$this->annotationReader->isRevised(get_class($entity), true)) { continue; } $this->setRevisionInfo($entity); $this->extraUpdates[spl_object_hash($entity)] = $entity; $persister = $this->uow->getEntityPersister(get_class($entity)); $this->updateData[spl_object_hash($entity)] = $this->prepareUpdateData($persister, $entity); if ($this->annotationReader->isDraft($entity) && $entity->isDraft()) { $this->resetRevisedData($entity); } } }