protected function initialize() { if (!$this->initialized) { $params = array(); $sql = 'SELECT MAX(' . $this->configuration->getRevisionFieldName() . ') as rev, '; $sql .= $this->configuration->getRevisionTypeFieldName() . ' AS revtype, '; $sql .= implode(', ', $this->metadata->getIdentifierColumnNames()) . ' '; if (isset($this->associationDefinition['indexBy'])) { $sql .= ', ' . $this->associationDefinition['indexBy'] . ' '; } $sql .= 'FROM ' . $this->configuration->getTablePrefix() . $this->metadata->table['name'] . $this->configuration->getTableSuffix() . ' t '; $sql .= 'WHERE ' . $this->configuration->getRevisionFieldName() . ' <= ' . $this->revision . ' '; foreach ($this->foreignKeys as $column => $value) { $sql .= 'AND ' . $column . ' = ? '; $params[] = $value; } //we check for revisions greater than current belonging to other entities $sql .= 'AND NOT EXISTS (SELECT * FROM ' . $this->configuration->getTablePrefix() . $this->metadata->table['name'] . $this->configuration->getTableSuffix() . ' st WHERE'; //ids foreach ($this->metadata->getIdentifierColumnNames() as $name) { $sql .= ' st.' . $name . ' = t.' . $name . ' AND'; } //foreigns $sql .= ' (('; //master entity query, not equals $notEqualParts = $nullParts = array(); foreach ($this->foreignKeys as $column => $value) { $notEqualParts[] = $column . ' <> ?'; $nullParts[] = $column . ' IS NULL'; $params[] = $value; } $sql .= implode(' AND ', $notEqualParts) . ') OR (' . implode(' AND ', $nullParts) . '))'; //revision $sql .= ' AND st.' . $this->configuration->getRevisionFieldName() . ' <= ' . $this->revision; $sql .= ' AND st.' . $this->configuration->getRevisionFieldName() . ' > t.' . $this->configuration->getRevisionFieldName(); $sql .= ') '; //end of check for for belonging to other entities //check for deleted revisions older than requested $sql .= 'AND NOT EXISTS (SELECT * FROM ' . $this->configuration->getTablePrefix() . $this->metadata->table['name'] . $this->configuration->getTableSuffix() . ' sd WHERE'; //ids foreach ($this->metadata->getIdentifierColumnNames() as $name) { $sql .= ' sd.' . $name . ' = t.' . $name . ' AND'; } //revision $sql .= ' sd.' . $this->configuration->getRevisionFieldName() . ' <= ' . $this->revision; $sql .= ' AND sd.' . $this->configuration->getRevisionFieldName() . ' > t.' . $this->configuration->getRevisionFieldName(); $sql .= ' AND sd.' . $this->configuration->getRevisionTypeFieldName() . ' = ?'; $params[] = 'DEL'; $sql .= ') '; //end check for deleted revisions older than requested $sql .= 'GROUP BY ' . implode(', ', $this->metadata->getIdentifierColumnNames()) . ' '; $sql .= 'HAVING ' . $this->configuration->getRevisionTypeFieldName() . ' <> ?'; //add rev type parameter $params[] = 'DEL'; $rows = $this->auditReader->getConnection()->fetchAll($sql, $params); foreach ($rows as $row) { $entity = array('rev' => $row['rev'], 'revtype' => $row['revtype']); unset($row['rev'], $row['revtype']); $entity['keys'] = $row; if (isset($this->associationDefinition['indexBy'])) { $key = $row[$this->associationDefinition['indexBy']]; unset($entity['keys'][$this->associationDefinition['indexBy']]); $this->entities[$key] = $entity; } else { $this->entities[] = $entity; } } $this->initialized = true; } }