Example #1
0
 /**
  * Find a class at the specific revision.
  *
  * This method does not require the revision to be exact but it also searches for an earlier revision
  * of this entity and always returns the latest revision below or equal the given revision
  *
  * @param  string $className
  * @param  mixed  $id
  * @param  int    $revision
  * @return Entity
  */
 public function find($className, $id, $revision)
 {
     if (!$this->metadataFactory->isAudited($className)) {
         throw Exception::notAudited($className);
     }
     $class = $this->em->getClassMetadata($className);
     $tableName = $this->config->getTablePrefix() . $class->table['name'] . $this->config->getTableSuffix();
     if (!is_array($id)) {
         $id = array($class->identifier[0] => $id);
     }
     $whereSQL = "e." . $this->config->getRevisionFieldName() . " <= ?";
     foreach ($class->identifier as $idField) {
         if (isset($class->fieldMappings[$idField])) {
             $columnName = $class->fieldMappings[$idField]['columnName'];
         } elseif (isset($class->associationMappings[$idField])) {
             $columnName = $class->associationMappings[$idField]['joinColumns'][0];
         }
         $whereSQL .= " AND " . $columnName . " = ?";
     }
     $columnList = "";
     $columnMap = array();
     foreach ($class->fieldNames as $columnName => $field) {
         if ($columnList) {
             $columnList .= ', ';
         }
         $type = Type::getType($class->fieldMappings[$field]['type']);
         $columnList .= $type->convertToPHPValueSQL($class->getQuotedColumnName($field, $this->platform), $this->platform) . ' AS ' . $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) {
             if ($columnList) {
                 $columnList .= ', ';
             }
             $columnList .= $sourceCol;
             $columnMap[$sourceCol] = $this->platform->getSQLResultCasing($sourceCol);
         }
     }
     $values = array_merge(array($revision), array_values($id));
     $query = "SELECT " . $columnList . " FROM " . $tableName . " e WHERE " . $whereSQL . " ORDER BY e.rev DESC";
     $row = $this->em->getConnection()->fetchAssoc($query, $values);
     if (!$row) {
         throw Exception::noRevisionFound($class->name, $id, $revision);
     }
     return $this->createEntity($class->name, $row);
 }