frontend, localization, bugfix

This commit is contained in:
2023-01-15 00:32:17 +01:00
parent 0418118841
commit 1d6ff17994
18 changed files with 297 additions and 84 deletions

View File

@@ -55,18 +55,20 @@ abstract class DatabaseEntity implements ArrayAccess, JsonSerializable {
}
public function jsonSerialize(?array $propertyNames = null): array {
$properties = (new \ReflectionClass(get_called_class()))->getProperties();
$reflectionClass = (new \ReflectionClass(get_called_class()));
$properties = $reflectionClass->getProperties();
$ignoredProperties = ["entityLogConfig", "customData"];
$jsonArray = [];
foreach ($properties as $property) {
$property->setAccessible(true);
$propertyName = $property->getName();
if (in_array($propertyName, $ignoredProperties)) {
continue;
}
if (!empty($property->getAttributes(Transient::class))) {
if (DatabaseEntityHandler::getAttribute($property, Transient::class)) {
continue;
}
@@ -93,6 +95,10 @@ abstract class DatabaseEntity implements ArrayAccess, JsonSerializable {
$value = $value->getTimestamp();
} else if ($value instanceof DatabaseEntity) {
$subPropertyNames = $propertyNames[$propertyName] ?? null;
if ($subPropertyNames === null && $value instanceof $this) {
$subPropertyNames = $propertyNames;
}
$value = $value->jsonSerialize($subPropertyNames);
} else if (is_array($value)) {
$subPropertyNames = $propertyNames[$propertyName] ?? null;
@@ -104,7 +110,7 @@ abstract class DatabaseEntity implements ArrayAccess, JsonSerializable {
}, $value);
}
$jsonArray[$property->getName()] = $value;
$jsonArray[$propertyName] = $value;
}
}
}

View File

@@ -315,6 +315,10 @@ class DatabaseEntityHandler implements Persistable {
return $this->properties[$property];
}
public function hasProperty(string $propertyName): bool {
return isset($this->properties[$propertyName]);
}
public static function getPrefixedRow(array $row, string $prefix): array {
$rel_row = [];
foreach ($row as $relKey => $relValue) {
@@ -325,7 +329,7 @@ class DatabaseEntityHandler implements Persistable {
return $rel_row;
}
private function getValueFromRow(array $row, string $propertyName, mixed &$value, bool $initEntities = false): bool {
private function getValueFromRow(array $row, string $propertyName, mixed &$value, int $fetchEntities = DatabaseEntityQuery::FETCH_NONE): bool {
$column = $this->columns[$propertyName] ?? null;
if (!$column) {
return false;
@@ -340,15 +344,22 @@ class DatabaseEntityHandler implements Persistable {
if ($column instanceof DateTimeColumn) {
$value = new \DateTime($value);
} else if ($column instanceof JsonColumn) {
$value = json_decode($value);
$value = json_decode($value, true);
} else if (isset($this->relations[$propertyName])) {
$relationHandler = $this->relations[$propertyName];
$relColumnPrefix = self::buildColumnName($propertyName) . "_";
if (array_key_exists($relColumnPrefix . "id", $row)) {
$relId = $row[$relColumnPrefix . "id"];
if ($relId !== null) {
if ($initEntities) {
$relationHandler = $this->relations[$propertyName];
$value = $relationHandler->entityFromRow(self::getPrefixedRow($row, $relColumnPrefix), [], true);
if ($fetchEntities !== DatabaseEntityQuery::FETCH_NONE) {
if ($this === $relationHandler) {
$value = DatabaseEntityQuery::fetchOne($this)
->fetchEntities($fetchEntities === DatabaseEntityQuery::FETCH_RECURSIVE)
->whereEq($this->getColumnName("id"), $relId)
->execute();
} else {
$value = $relationHandler->entityFromRow(self::getPrefixedRow($row, $relColumnPrefix), [], true);
}
} else {
return false;
}
@@ -362,10 +373,17 @@ class DatabaseEntityHandler implements Persistable {
}
}
if ($value === null) {
$defaultValue = self::getAttribute($this->properties[$propertyName], DefaultValue::class);
if ($defaultValue) {
$value = $defaultValue->getValue();
}
}
return true;
}
public function entityFromRow(array $row, array $additionalColumns = [], bool $initEntities = false): ?DatabaseEntity {
public function entityFromRow(array $row, array $additionalColumns = [], int $fetchEntities = DatabaseEntityQuery::FETCH_NONE): ?DatabaseEntity {
try {
$constructorClass = $this->entityClass;
@@ -384,7 +402,8 @@ class DatabaseEntityHandler implements Persistable {
}
foreach ($this->properties as $property) {
if ($this->getValueFromRow($row, $property->getName(), $value, $initEntities)) {
$propertyName = $property->getName();
if ($this->getValueFromRow($row, $propertyName, $value, $fetchEntities)) {
$property->setAccessible(true);
$property->setValue($entity, $value);
}
@@ -405,6 +424,7 @@ class DatabaseEntityHandler implements Persistable {
$this->properties["id"]->setAccessible(true);
$this->properties["id"]->setValue($entity, $row["id"]);
$entity->postFetch($this->sql, $row);
return $entity;
} catch (\Exception $exception) {
@@ -512,9 +532,13 @@ class DatabaseEntityHandler implements Persistable {
return $success;
}
public function fetchNMRelations(array $entities, bool $recursive = false) {
public function fetchNMRelations(array $entities, int $fetchEntities = DatabaseEntityQuery::FETCH_DIRECT) {
if ($recursive) {
if ($fetchEntities === DatabaseEntityQuery::FETCH_NONE) {
return;
}
if ($fetchEntities === DatabaseEntityQuery::FETCH_RECURSIVE) {
foreach ($entities as $entity) {
foreach ($this->relations as $propertyName => $relHandler) {
$property = $this->properties[$propertyName];
@@ -549,10 +573,7 @@ class DatabaseEntityHandler implements Persistable {
->addSelectValue(new Column($thisIdColumn))
->where(new CondIn(new Column($thisIdColumn), $entityIds));
if ($recursive) {
$relEntityQuery->fetchEntities(true);
}
$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());
@@ -563,7 +584,7 @@ class DatabaseEntityHandler implements Persistable {
foreach ($rows as $row) {
$relId = $row["id"];
if (!isset($relEntities[$relId])) {
$relEntity = $otherHandler->entityFromRow($row, [], $recursive);
$relEntity = $otherHandler->entityFromRow($row, [], $fetchEntities);
$relEntities[$relId] = $relEntity;
}
@@ -575,6 +596,7 @@ class DatabaseEntityHandler implements Persistable {
$property->setValue($thisEntity, $targetArray);
}
} else if ($nmRelation instanceof NMRelationReference) {
$otherHandler = $nmRelation->getRelHandler();
$thisIdColumn = $otherHandler->getColumnName($nmRelation->getThisProperty(), false);
$relIdColumn = $otherHandler->getColumnName($nmRelation->getRefProperty(), false);
@@ -582,10 +604,7 @@ class DatabaseEntityHandler implements Persistable {
$relEntityQuery = DatabaseEntityQuery::fetchAll($otherHandler)
->where(new CondIn(new Column($thisIdColumn), $entityIds));
if ($recursive) {
$relEntityQuery->fetchEntities(true);
}
$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());
@@ -596,7 +615,7 @@ class DatabaseEntityHandler implements Persistable {
$thisIdProperty->setAccessible(true);
foreach ($rows as $row) {
$relEntity = $otherHandler->entityFromRow($row, [], $recursive);
$relEntity = $otherHandler->entityFromRow($row, [], $fetchEntities);
$thisEntity = $entities[$row[$thisIdColumn]];
$thisIdProperty->setValue($relEntity, $thisEntity);
$targetArray = $property->getValue($thisEntity);
@@ -609,10 +628,11 @@ class DatabaseEntityHandler implements Persistable {
continue;
}
if ($recursive) {
// TODO whats that here lol
if ($fetchEntities === DatabaseEntityQuery::FETCH_RECURSIVE) {
foreach ($entities as $entity) {
$relEntities = $property->getValue($entity);
$otherHandler->fetchNMRelations($relEntities);
// $otherHandler->fetchNMRelations($relEntities, $fetchEntities);
}
}
}

View File

@@ -94,7 +94,9 @@ class DatabaseEntityQuery extends Select {
$relIndex = 1;
foreach ($this->handler->getRelations() as $propertyName => $relationHandler) {
$this->fetchRelation($propertyName, $this->handler->getTableName(), $this->handler, $relationHandler, $relIndex, $recursive);
if ($this->handler !== $relationHandler) {
$this->fetchRelation($propertyName, $this->handler->getTableName(), $this->handler, $relationHandler, $relIndex, $recursive);
}
}
return $this;
@@ -117,7 +119,6 @@ class DatabaseEntityQuery extends Select {
$alias = "t$relIndex"; // t1, t2, t3, ...
$relIndex++;
if ($isNullable) {
$this->leftJoin($referencedTable, "$tableName.$foreignColumnName", "$alias.id", $alias);
} else {
@@ -153,21 +154,21 @@ class DatabaseEntityQuery extends Select {
if ($this->resultType === SQL::FETCH_ALL) {
$entities = [];
foreach ($res as $row) {
$entity = $this->handler->entityFromRow($row, $this->additionalColumns, $this->fetchSubEntities !== self::FETCH_NONE);
$entity = $this->handler->entityFromRow($row, $this->additionalColumns, $this->fetchSubEntities);
if ($entity) {
$entities[$entity->getId()] = $entity;
}
}
if ($this->fetchSubEntities !== self::FETCH_NONE) {
$this->handler->fetchNMRelations($entities, $this->fetchSubEntities === self::FETCH_RECURSIVE);
$this->handler->fetchNMRelations($entities, $this->fetchSubEntities);
}
return $entities;
} else if ($this->resultType === SQL::FETCH_ONE) {
$entity = $this->handler->entityFromRow($res, $this->additionalColumns, $this->fetchSubEntities !== self::FETCH_NONE);
$entity = $this->handler->entityFromRow($res, $this->additionalColumns, $this->fetchSubEntities);
if ($entity instanceof DatabaseEntity && $this->fetchSubEntities !== self::FETCH_NONE) {
$this->handler->fetchNMRelations([$entity->getId() => $entity], $this->fetchSubEntities === self::FETCH_RECURSIVE);
$this->handler->fetchNMRelations([$entity->getId() => $entity], $this->fetchSubEntities);
}
return $entity;