public function activateAction()
 {
     $console = $this->getServiceLocator()->get('console');
     // Make sure that we are running in a console and the user has not tricked our
     // application into running this action from a public web server.
     $request = $this->getRequest();
     if (!$request instanceof ConsoleRequest) {
         throw new RuntimeException('You can only use this action from a console.');
     }
     $fieldName = $this->params()->fromRoute('field');
     $entityName = $this->params()->fromRoute('entity');
     $comment = $this->params()->fromRoute('comment');
     $queryBuilder = $this->getAuditObjectManager()->createQueryBuilder();
     $queryBuilder->select('field')->from('ZF\\Doctrine\\Audit\\Entity\\Field', 'field')->innerJoin('field.targetEntity', 'targetEntity')->andWhere('field.name = :fieldName')->andWhere('targetEntity.name = :entityName')->setParameter('entityName', $entityName)->setParameter('fieldName', $fieldName);
     $field = $queryBuilder->getQuery()->getOneOrNullResult();
     if (!$field) {
         $console->write("Field was not found\n", Color::RED);
         return;
     }
     $fieldStatus = $this->getAuditObjectManager()->getRepository('ZF\\Doctrine\\Audit\\Entity\\FieldStatus')->findOneBy(['name' => 'active']);
     $revision = new Entity\Revision();
     $revision->setCreatedAt(new DateTime());
     $revision->setComment($comment ?? 'Activated field from console command');
     $this->getAuditObjectManager()->persist($revision);
     $fieldRevision = new Entity\FieldRevision();
     $fieldRevision->setFieldStatus($fieldStatus);
     $fieldRevision->setField($field);
     $fieldRevision->setRevision($revision);
     $this->getAuditObjectManager()->persist($fieldRevision);
     $this->getAuditObjectManager()->flush();
     $console->write("Field has been activated\n", Color::GREEN);
 }
 public function testGettersAndSetters()
 {
     $entity = new Revision();
     $this->assertLessThanOrEqual(new \DateTime(), $entity->getTimestamp());
     $userClass = \ZF\Doctrine\Audit\Module::getModuleOptions()->getUserEntityClassName();
     $user = new $userClass();
     $this->assertEquals($entity, $entity->setUser($user));
     $this->assertEquals($user, $entity->getUser());
     $entity->setComment('Test revision entity setter and getter');
     $this->assertEquals('Test revision entity setter and getter', $entity->getComment());
 }
 private function buildRevision()
 {
     if ($this->revision) {
         return;
     }
     $revision = new RevisionEntity();
     $moduleOptions = \ZF\Doctrine\Audit\Module::getModuleOptions();
     if ($moduleOptions->getUser()) {
         $revision->setUser($moduleOptions->getUser());
     }
     $comment = $moduleOptions->getAuditService()->getComment();
     $revision->setComment($comment);
     $this->revision = $revision;
 }
 public function load(ObjectManager $auditObjectManager)
 {
     $config = $this->getServiceLocator()->get('Config')['zf-doctrine-audit'];
     $objectManager = $this->getServiceLocator()->get($config['target_object_manager']);
     $fieldStatusActive = $auditObjectManager->getRepository('ZF\\Doctrine\\Audit\\Entity\\FieldStatus')->findOneBy(['name' => 'active']);
     // Create a revision to associate with field revision
     $revision = new Entity\Revision();
     $revision->setCreatedAt(new DateTime());
     $revision->setComment('Data Fixture Import');
     $auditObjectManager->persist($revision);
     foreach ($config['entities'] as $className => $route) {
         $targetEntity = $auditObjectManager->getRepository('ZF\\Doctrine\\Audit\\Entity\\TargetEntity')->findOneBy(['name' => $className]);
         if (!$targetEntity) {
             $auditEntityClassName = $auditObjectManager->getRepository('ZF\\Doctrine\\Audit\\Entity\\AuditEntity')->generateClassName($className);
             $auditEntity = new Entity\AuditEntity();
             $auditEntity->setName($auditEntityClassName);
             $auditEntity->setTableName($auditObjectManager->getClassMetadata($auditEntityClassName)->getTableName());
             $targetEntity = new Entity\TargetEntity();
             $targetEntity->setAuditEntity($auditEntity);
             $targetEntity->setName($className);
             $targetEntity->setTableName($objectManager->getClassMetadata($className)->getTableName());
             $identifiers = $objectManager->getClassMetadata($className)->getIdentifierFieldNames();
             foreach ($identifiers as $fieldName) {
                 $identifier = new Entity\Identifier();
                 $identifier->setTargetEntity($targetEntity);
                 $identifier->setFieldName($fieldName);
                 $identifier->setColumnName($objectManager->getClassMetadata($className)->getColumnName($fieldName));
                 $auditObjectManager->persist($identifier);
             }
             // Add Fields
             $fields = $objectManager->getClassMetadata($className)->getFieldNames();
             foreach ($fields as $fieldName) {
                 $field = new Entity\Field();
                 $field->setTargetEntity($targetEntity);
                 $field->setName($fieldName);
                 $field->setColumnName($objectManager->getClassMetadata($className)->getColumnName($fieldName));
                 $fieldRevision = new Entity\FieldRevision();
                 $fieldRevision->setFieldStatus($fieldStatusActive);
                 $fieldRevision->setField($field);
                 $fieldRevision->setRevision($revision);
                 $auditObjectManager->persist($field);
                 $auditObjectManager->persist($fieldRevision);
             }
             // Add Associations to Fields
             $associations = $objectManager->getClassMetadata($className)->getAssociationNames();
             foreach ($associations as $fieldName) {
                 $associationMapping = $objectManager->getClassMetadata($className)->getAssociationMapping($fieldName);
                 if (!isset($associationMapping['joinColumns'])) {
                     continue;
                 }
                 if (sizeof($associationMapping['joinColumns']) != 1) {
                     throw new Exception('Unable to handle > 1 join column per association');
                 }
                 $field = new Entity\Field();
                 $field->setTargetEntity($targetEntity);
                 $field->setName($fieldName);
                 $field->setColumnName(array_shift($associationMapping['joinColumns'])['name']);
                 $fieldRevision = new Entity\FieldRevision();
                 $fieldRevision->setFieldStatus($fieldStatusActive);
                 $fieldRevision->setField($field);
                 $fieldRevision->setRevision($revision);
                 $auditObjectManager->persist($field);
                 $auditObjectManager->persist($fieldRevision);
             }
             $auditObjectManager->persist($auditEntity);
             $auditObjectManager->persist($targetEntity);
         }
     }
     $auditObjectManager->flush();
 }