frontend & backend update

This commit is contained in:
2023-01-16 21:47:23 +01:00
parent 1d6ff17994
commit 4cec531a25
51 changed files with 1010 additions and 571 deletions

View File

@@ -57,6 +57,12 @@ abstract class DatabaseEntity implements ArrayAccess, JsonSerializable {
public function jsonSerialize(?array $propertyNames = null): array {
$reflectionClass = (new \ReflectionClass(get_called_class()));
$properties = $reflectionClass->getProperties();
while ($reflectionClass->getParentClass()->getName() !== DatabaseEntity::class) {
$reflectionClass = $reflectionClass->getParentClass();
$properties = array_merge($reflectionClass->getProperties(), $properties);
}
$ignoredProperties = ["entityLogConfig", "customData"];
$jsonArray = [];

View File

@@ -524,7 +524,15 @@ class DatabaseEntityHandler implements Persistable {
$thisIdProperty->setValue($relEntity, $entity);
}
$success = $otherHandler->getInsertQuery($relEntities)->execute() && $success;
$statement = $otherHandler->getInsertQuery($relEntities);
if ($ignoreExisting) {
$columns = $nmRelation->getRefColumns();
$statement->onDuplicateKeyStrategy(new UpdateStrategy($columns, [
$thisIdColumn => $entity->getId()
]));
}
$success = $statement->execute() && $success;
}
}
}
@@ -557,6 +565,10 @@ class DatabaseEntityHandler implements Persistable {
}
$entityIds = array_keys($entities);
if (empty($entityIds)) {
return;
}
foreach ($this->nmRelations as $nmProperty => $nmRelation) {
$nmTable = $nmRelation->getTableName();
$property = $this->properties[$nmProperty];
@@ -599,29 +611,27 @@ class DatabaseEntityHandler implements Persistable {
$otherHandler = $nmRelation->getRelHandler();
$thisIdColumn = $otherHandler->getColumnName($nmRelation->getThisProperty(), false);
$relIdColumn = $otherHandler->getColumnName($nmRelation->getRefProperty(), false);
if (!empty($entityIds)) {
$relEntityQuery = DatabaseEntityQuery::fetchAll($otherHandler)
->where(new CondIn(new Column($thisIdColumn), $entityIds));
$relIdColumn = $otherHandler->getColumnName($nmRelation->getRefProperty(), false);
$relEntityQuery = DatabaseEntityQuery::fetchAll($otherHandler)
->where(new CondIn(new Column($thisIdColumn), $entityIds));
$relEntityQuery->fetchEntities($fetchEntities === DatabaseEntityQuery::FETCH_RECURSIVE);
$rows = $relEntityQuery->executeSQL();
if (!is_array($rows)) {
$this->logger->error("Error fetching n:m relations from table: '$nmTable': " . $this->sql->getLastError());
return;
}
$relEntityQuery->fetchEntities($fetchEntities === DatabaseEntityQuery::FETCH_RECURSIVE);
$rows = $relEntityQuery->executeSQL();
if (!is_array($rows)) {
$this->logger->error("Error fetching n:m relations from table: '$nmTable': " . $this->sql->getLastError());
return;
}
$thisIdProperty = $otherHandler->properties[$nmRelation->getThisProperty()];
$thisIdProperty->setAccessible(true);
$thisIdProperty = $otherHandler->properties[$nmRelation->getThisProperty()];
$thisIdProperty->setAccessible(true);
foreach ($rows as $row) {
$relEntity = $otherHandler->entityFromRow($row, [], $fetchEntities);
$thisEntity = $entities[$row[$thisIdColumn]];
$thisIdProperty->setValue($relEntity, $thisEntity);
$targetArray = $property->getValue($thisEntity);
$targetArray[$row[$relIdColumn]] = $relEntity;
$property->setValue($thisEntity, $targetArray);
}
foreach ($rows as $row) {
$relEntity = $otherHandler->entityFromRow($row, [], $fetchEntities);
$thisEntity = $entities[$row[$thisIdColumn]];
$thisIdProperty->setValue($relEntity, $thisEntity);
$targetArray = $property->getValue($thisEntity);
$targetArray[$row[$relIdColumn]] = $relEntity;
$property->setValue($thisEntity, $targetArray);
}
} else {
$this->logger->error("fetchNMRelations for type '" . get_class($nmRelation) . "' is not implemented");

View File

@@ -94,7 +94,7 @@ class DatabaseEntityQuery extends Select {
$relIndex = 1;
foreach ($this->handler->getRelations() as $propertyName => $relationHandler) {
if ($this->handler !== $relationHandler) {
if ($this->handler !== $relationHandler || !$recursive) {
$this->fetchRelation($propertyName, $this->handler->getTableName(), $this->handler, $relationHandler, $relIndex, $recursive);
}
}

View File

@@ -36,6 +36,13 @@ class NMRelationReference implements Persistable {
return $this->refProperty;
}
public function getRefColumns(): array {
return [
$this->handler->getColumnName($this->getThisProperty(), false),
$this->handler->getColumnName($this->getRefProperty(), false),
];
}
public function getRelHandler(): DatabaseEntityHandler {
return $this->handler;
}