Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I have added CTI support in this fork #139

Closed
wants to merge 9 commits into from
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,9 +238,12 @@ This provides you with a few different routes:
* ```simple_things_entity_audit_viewentity_detail``` - Displays the data for the specified entity at the specified revision
* ```simple_things_entity_audit_compare``` - Allows you to compare the changes of an entity between 2 revisions

## Done TODOS

* It DOES support work with Joined-Table-Inheritance - tested but not many. (Single Table Inheritance should work, but not tested)
*
## TODOS

* Currently only works with auto-increment databases
* Proper metadata mapping is necessary, allow to disable versioning for fields and associations.
* It does NOT work with Joined-Table-Inheritance (Single Table Inheritance should work, but not tested)
* Many-To-Many associations are NOT versioned
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"description": "Audit for Doctrine Entities",
"require": {
"doctrine/dbal": "~2.5",
"doctrine/orm": "~2.2"
"doctrine/orm": "~2.2,<2.5"
},
"require-dev": {
"symfony/framework-bundle": "~2.3",
Expand Down
131 changes: 63 additions & 68 deletions src/SimpleThings/EntityAudit/AuditReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,16 +216,10 @@ public function find($className, $id, $revision, array $options = array())
}

$whereSQL = "e." . $this->config->getRevisionFieldName() ." <= ?";

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];
}

$whereSQL .= " AND e." . $columnName . " = ?";
if ($whereSQL) {
$whereSQL .= ' AND ';
}
$whereSQL .= $this->createWhereSQLForId($class);

$columnList = array('e.'.$this->config->getRevisionTypeFieldName());
$columnMap = array();
Expand Down Expand Up @@ -299,6 +293,14 @@ public function find($className, $id, $revision, array $options = array())
return $this->createEntity($class->name, $row, $revision);
}

private function isClassField($class, $field){
if (!array_key_exists($field, $class->reflFields)) {
return false;
}
$refField = $class->reflFields[$field];
return $refField->class === $class->reflClass->name || in_array($field, $class->identifier);

}
/**
* Simplified and stolen code from UnitOfWork::createEntity.
*
Expand All @@ -316,6 +318,7 @@ public function find($className, $id, $revision, array $options = array())
*/
private function createEntity($className, array $data, $revision)
{
/** @var ClassMetadata $class */
$class = $this->em->getClassMetadata($className);

//lookup revisioned entity cache
Expand All @@ -335,27 +338,27 @@ private function createEntity($className, array $data, $revision)
}

if (!$class->isInheritanceTypeNone()) {
if (!isset($data[$class->discriminatorColumn['name']])) {
throw new \RuntimeException('Expecting discriminator value in data set.');
}
$discriminator = $data[$class->discriminatorColumn['name']];
if (!isset($class->discriminatorMap[$discriminator])) {
throw new \RuntimeException("No mapping found for [{$discriminator}].");
}
if (!isset($data[$class->discriminatorColumn['name']])) {
throw new \RuntimeException('Expecting discriminator value in data set.');
}
$discriminator = $data[$class->discriminatorColumn['name']];
if (!isset($class->discriminatorMap[$discriminator])) {
throw new \RuntimeException("No mapping found for [{$discriminator}].");
}

if ($class->discriminatorValue) {
$entity = $this->em->getClassMetadata($class->discriminatorMap[$discriminator])
->newInstance();
} else {
//a complex case when ToOne binding is against AbstractEntity having no discriminator
$pk = array();
if ($class->discriminatorValue) {
$entity = $this->em->getClassMetadata($class->discriminatorMap[$discriminator])
->newInstance();
} else {
//a complex case when ToOne binding is against AbstractEntity having no discriminator
$pk = array();

foreach ($class->identifier as $field) {
$pk[$class->getColumnName($field)] = $data[$field];
}
foreach ($class->identifier as $field) {
$pk[$class->getColumnName($field)] = $data[$field];
}

return $this->find($class->discriminatorMap[$discriminator], $pk, $revision);
}
return $this->find($class->discriminatorMap[$discriminator], $pk, $revision);
}
} else {
$entity = $class->newInstance();
}
Expand Down Expand Up @@ -524,11 +527,15 @@ public function findEntitiesChangedAtRevision($revision)

$changedEntities = array();
foreach ($auditedEntities AS $className) {
/** @var ClassMetadata $class */
$class = $this->em->getClassMetadata($className);

if ($class->isInheritanceTypeSingleTable() && count($class->subClasses) > 0) {
continue;
}
if ($class->isInheritanceTypeJoined() && $class->rootEntityName == $class->name && $class->getReflectionClass()->isAbstract()) {
continue;
}

$tableName = $this->config->getTablePrefix() . $class->table['name'] . $this->config->getTableSuffix();
$params = array();
Expand Down Expand Up @@ -571,6 +578,8 @@ public function findEntitiesChangedAtRevision($revision)
foreach ($class->getIdentifierColumnNames() as $name) {
$joinSql .= " AND re.$name = e.$name";
}
} else if ($class->isInheritanceTypeJoined() && $class->rootEntityName == $class->name) {
$columnList .= ', e.' . $class->discriminatorColumn['name'];
}

$query = "SELECT " . $columnList . " FROM " . $tableName . " e " . $joinSql . " WHERE " . $whereSQL;
Expand Down Expand Up @@ -635,20 +644,7 @@ public function findRevisions($className, $id)
$id = array($class->identifier[0] => $id);
}

$whereSQL = "";
foreach ($class->identifier AS $idField) {
if (isset($class->fieldMappings[$idField])) {
if ($whereSQL) {
$whereSQL .= " AND ";
}
$whereSQL .= "e." . $class->fieldMappings[$idField]['columnName'] . " = ?";
} else if (isset($class->associationMappings[$idField])) {
if ($whereSQL) {
$whereSQL .= " AND ";
}
$whereSQL .= "e." . $class->associationMappings[$idField]['joinColumns'][0] . " = ?";
}
}
$whereSQL = $this->createWhereSQLForId($class);

$query = "SELECT r.* FROM " . $this->config->getRevisionTableName() . " r " .
"INNER JOIN " . $tableName . " e ON r.id = e." . $this->config->getRevisionFieldName() . " WHERE " . $whereSQL . " ORDER BY r.id DESC";
Expand Down Expand Up @@ -687,20 +683,7 @@ public function getCurrentRevision($className, $id)
$id = array($class->identifier[0] => $id);
}

$whereSQL = "";
foreach ($class->identifier AS $idField) {
if (isset($class->fieldMappings[$idField])) {
if ($whereSQL) {
$whereSQL .= " AND ";
}
$whereSQL .= "e." . $class->fieldMappings[$idField]['columnName'] . " = ?";
} else if (isset($class->associationMappings[$idField])) {
if ($whereSQL) {
$whereSQL .= " AND ";
}
$whereSQL .= "e." . $class->associationMappings[$idField]['joinColumns'][0] . " = ?";
}
}
$whereSQL = $this->createWhereSQLForId($class);

$query = "SELECT e.".$this->config->getRevisionFieldName()." FROM " . $tableName . " e " .
" WHERE " . $whereSQL . " ORDER BY e.".$this->config->getRevisionFieldName()." DESC";
Expand Down Expand Up @@ -770,20 +753,8 @@ public function getEntityHistory($className, $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 = $this->createWhereSQLForId($class);

$whereSQL = implode(' AND ', $whereId);
$columnList = array($this->config->getRevisionFieldName());
$columnMap = array();

Expand Down Expand Up @@ -819,4 +790,28 @@ public function getEntityHistory($className, $id)

return $result;
}


/**
* @param $class
* @return string
*/
private function createWhereSQLForId($class)
{
$whereSQL = "";
foreach ($class->identifier AS $idField) {
if (isset($class->fieldMappings[$idField])) {
if ($whereSQL) {
$whereSQL .= " AND ";
}
$whereSQL .= "e." . $class->fieldMappings[$idField]['columnName'] . " = ?";
} else if (isset($class->associationMappings[$idField])) {
if ($whereSQL) {
$whereSQL .= " AND ";
}
$whereSQL .= "e." . $class->associationMappings[$idField]['joinColumns'][0] . " = ?";
}
}
return $whereSQL;
}
}