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

Multiple queries to tables have been replaced with a single one #612

Merged
merged 1 commit into from
Apr 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 31 additions & 24 deletions src/EventListener/LogRevisionsListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ public function postFlush(PostFlushEventArgs $eventArgs): void
continue;
}

$sql = 'UPDATE '.$this->config->getTableName($meta).' ';
$params = $types = [];

foreach ($updateData[$meta->table['name']] as $column => $value) {
$field = $meta->getFieldName($column);
$fieldName = $meta->getFieldForColumn($column);
Expand All @@ -128,13 +131,15 @@ public function postFlush(PostFlushEventArgs $eventArgs): void
}
}

$sql = 'UPDATE '.$this->config->getTableName($meta).' '.
'SET '.$field.' = '.$placeholder.' '.
'WHERE '.$this->config->getRevisionFieldName().' = ? ';
if ($column === array_key_first($updateData[$meta->table['name']])) {
$sql .= 'SET ';
} else {
$sql = trim($sql).', ';
}

$params = [$value, $this->getRevisionId($conn)];
$sql .= $field.' = '.$placeholder.' ';

$types = [];
$params[] = $value;

if (\array_key_exists($column, $meta->fieldNames)) {
$types[] = $meta->getTypeOfField($fieldName);
Expand All @@ -161,29 +166,31 @@ public function postFlush(PostFlushEventArgs $eventArgs): void

$types[] = $type;
}
}

$types[] = $this->config->getRevisionIdFieldType();

foreach ($meta->identifier as $idField) {
if (isset($meta->fieldMappings[$idField])) {
$columnName = $meta->fieldMappings[$idField]['columnName'];
$types[] = $meta->fieldMappings[$idField]['type'];
} elseif (isset($meta->associationMappings[$idField]['joinColumns'])) {
$columnName = $meta->associationMappings[$idField]['joinColumns'][0]['name'];
$types[] = $meta->associationMappings[$idField]['type'];
} else {
throw new \RuntimeException('column name not found for'.$idField);
}

$reflField = $meta->reflFields[$idField];
\assert(null !== $reflField);
$params[] = $reflField->getValue($entity);

$sql .= ' AND '.$columnName.' = ?';
$sql .= 'WHERE '.$this->config->getRevisionFieldName().' = ? ';
$params[] = $this->getRevisionId($conn);
$types[] = $this->config->getRevisionIdFieldType();

foreach ($meta->identifier as $idField) {
if (isset($meta->fieldMappings[$idField])) {
$columnName = $meta->fieldMappings[$idField]['columnName'];
$types[] = $meta->fieldMappings[$idField]['type'];
} elseif (isset($meta->associationMappings[$idField]['joinColumns'])) {
$columnName = $meta->associationMappings[$idField]['joinColumns'][0]['name'];
$types[] = $meta->associationMappings[$idField]['type'];
} else {
throw new \RuntimeException('column name not found for'.$idField);
}

$em->getConnection()->executeQuery($sql, $params, $types);
$reflField = $meta->reflFields[$idField];
\assert(null !== $reflField);
$params[] = $reflField->getValue($entity);

$sql .= ' AND '.$columnName.' = ?';
}

$em->getConnection()->executeQuery($sql, $params, $types);
}

foreach ($this->deferredChangedManyToManyEntityRevisionsToPersist as $deferredChangedManyToManyEntityRevisionToPersist) {
Expand Down
Loading