From dee9d8c34ed3b2003d7f22048350f0f6f1c062f4 Mon Sep 17 00:00:00 2001 From: Roman Date: Wed, 11 Jan 2023 13:30:08 +0100 Subject: [PATCH] nm bugfix + pagination default order params --- Core/API/Traits/Pagination.trait.php | 6 +-- .../DatabaseEntity/Attribute/Multiple.php | 3 ++ .../Attribute/MultipleReference.class.php | 4 +- .../Controller/DatabaseEntity.class.php | 6 ++- .../Controller/DatabaseEntityHandler.php | 41 ++++++++++++++----- 5 files changed, 43 insertions(+), 17 deletions(-) diff --git a/Core/API/Traits/Pagination.trait.php b/Core/API/Traits/Pagination.trait.php index 0714411..f46adf9 100644 --- a/Core/API/Traits/Pagination.trait.php +++ b/Core/API/Traits/Pagination.trait.php @@ -12,12 +12,12 @@ use Core\Objects\DatabaseEntity\User; trait Pagination { - static function getPaginationParameters(array $orderColumns): array { + static function getPaginationParameters(array $orderColumns, string $defaultOrderBy = "id", string $defaultSortOrder = "asc"): array { return [ 'page' => new Parameter('page', Parameter::TYPE_INT, true, 1), 'count' => new Parameter('count', Parameter::TYPE_INT, true, 20), - 'orderBy' => new StringType('orderBy', -1, true, "id", $orderColumns), - 'sortOrder' => new StringType('sortOrder', -1, true, 'asc', ['asc', 'desc']), + 'orderBy' => new StringType('orderBy', -1, true, $defaultOrderBy, $orderColumns), + 'sortOrder' => new StringType('sortOrder', -1, true, $defaultSortOrder, ['asc', 'desc']), ]; } diff --git a/Core/Objects/DatabaseEntity/Attribute/Multiple.php b/Core/Objects/DatabaseEntity/Attribute/Multiple.php index 13bc607..f1eff92 100644 --- a/Core/Objects/DatabaseEntity/Attribute/Multiple.php +++ b/Core/Objects/DatabaseEntity/Attribute/Multiple.php @@ -2,6 +2,9 @@ namespace Core\Objects\DatabaseEntity\Attribute; +// Unmanaged NM table, e.g. #[Multiple(Group::class)] for property 'groups' in User::class will create a +// table called NM_User_groups with just two columns (user_id, group_id) + #[\Attribute(\Attribute::TARGET_PROPERTY)] class Multiple { diff --git a/Core/Objects/DatabaseEntity/Attribute/MultipleReference.class.php b/Core/Objects/DatabaseEntity/Attribute/MultipleReference.class.php index 036faff..9fc21f2 100644 --- a/Core/Objects/DatabaseEntity/Attribute/MultipleReference.class.php +++ b/Core/Objects/DatabaseEntity/Attribute/MultipleReference.class.php @@ -2,8 +2,8 @@ namespace Core\Objects\DatabaseEntity\Attribute; - -namespace Core\Objects\DatabaseEntity\Attribute; +// Managed NM table, e.g. #[MultipleReference(Y::class, "x", "z")] in X::class will use +// the table of Y::class and lookup values by column "x_id" and create an array with keys of "z_id" holding a reference of Y #[\Attribute(\Attribute::TARGET_PROPERTY)] class MultipleReference { diff --git a/Core/Objects/DatabaseEntity/Controller/DatabaseEntity.class.php b/Core/Objects/DatabaseEntity/Controller/DatabaseEntity.class.php index 5ddd590..ce3fc93 100644 --- a/Core/Objects/DatabaseEntity/Controller/DatabaseEntity.class.php +++ b/Core/Objects/DatabaseEntity/Controller/DatabaseEntity.class.php @@ -22,7 +22,7 @@ abstract class DatabaseEntity implements ArrayAccess, JsonSerializable { private static array $handlers = []; protected ?int $id; - #[Transient] protected array $customData = []; + #[Transient] public array $customData = []; public function __construct(?int $id = null) { $this->id = $id; @@ -101,6 +101,10 @@ abstract class DatabaseEntity implements ArrayAccess, JsonSerializable { } } + if ($propertyNames === null) { + $jsonArray = array_merge($jsonArray, $this->customData); + } + return $jsonArray; } diff --git a/Core/Objects/DatabaseEntity/Controller/DatabaseEntityHandler.php b/Core/Objects/DatabaseEntity/Controller/DatabaseEntityHandler.php index 079da5b..8c3b3e0 100644 --- a/Core/Objects/DatabaseEntity/Controller/DatabaseEntityHandler.php +++ b/Core/Objects/DatabaseEntity/Controller/DatabaseEntityHandler.php @@ -149,7 +149,12 @@ class DatabaseEntityHandler implements Persistable { } } } else if ($propertyTypeName === 'int') { - $this->columns[$propertyName] = new IntColumn($columnName, $nullable, $defaultValue); + $bigInt = self::getAttribute($property, BigInt::class); + if ($bigInt) { + $this->columns[$propertyName] = new BigIntColumn($columnName, $nullable, $defaultValue, $bigInt->isUnsigned()); + } else { + $this->columns[$propertyName] = new IntColumn($columnName, $nullable, $defaultValue); + } } else if ($propertyTypeName === 'float') { $this->columns[$propertyName] = new FloatColumn($columnName, $nullable, $defaultValue); } else if ($propertyTypeName === 'double') { @@ -259,11 +264,19 @@ class DatabaseEntityHandler implements Persistable { return $this->relations; } - public function getColumnName(string $property): string { - if ($property === "id") { - return "$this->tableName.id"; + public function getColumnName(string $property, bool $withTableName = true): string { + if ($withTableName) { + if ($property === "id") { + return "$this->tableName.id"; + } else { + return $this->tableName . "." . $this->columns[$property]->getName(); + } } else { - return $this->tableName . "." . $this->columns[$property]->getName(); + if ($property === "id") { + return "id"; + } else { + return $this->columns[$property]->getName(); + } } } @@ -295,6 +308,10 @@ class DatabaseEntityHandler implements Persistable { return $this->nmRelations[$property]; } + public function getProperty(string $property): \ReflectionProperty { + return $this->properties[$property]; + } + public static function getPrefixedRow(array $row, string $prefix): array { $rel_row = []; foreach ($row as $relKey => $relValue) { @@ -407,8 +424,9 @@ class DatabaseEntityHandler implements Persistable { $otherHandler = $nmRelation->getOtherHandler($this); $refIdColumn = $nmRelation->getIdColumn($otherHandler); } else if ($nmRelation instanceof NMRelationReference) { - $thisIdColumn = self::buildColumnName($nmRelation->getThisProperty()); - $refIdColumn = self::buildColumnName($nmRelation->getRefProperty()); + $otherHandler = $nmRelation->getRelHandler(); + $thisIdColumn = $otherHandler->getColumnName($nmRelation->getThisProperty(), false); + $refIdColumn = $otherHandler->getColumnName($nmRelation->getRefProperty(), false); } else { throw new \Exception("updateNM not implemented for type: " . get_class($nmRelation)); } @@ -449,8 +467,9 @@ class DatabaseEntityHandler implements Persistable { $thisIdColumn = $nmRelation->getIdColumn($this); $refIdColumn = $nmRelation->getIdColumn($otherHandler); } else if ($nmRelation instanceof NMRelationReference) { - $thisIdColumn = self::buildColumnName($nmRelation->getThisProperty()); - $refIdColumn = self::buildColumnName($nmRelation->getRefProperty()); + $otherHandler = $nmRelation->getRelHandler(); + $thisIdColumn = $otherHandler->getColumnName($nmRelation->getThisProperty(), false); + $refIdColumn = $otherHandler->getColumnName($nmRelation->getRefProperty(), false); } else { throw new \Exception("insertNM not implemented for type: " . get_class($nmRelation)); } @@ -550,8 +569,8 @@ class DatabaseEntityHandler implements Persistable { } } else if ($nmRelation instanceof NMRelationReference) { $otherHandler = $nmRelation->getRelHandler(); - $thisIdColumn = self::buildColumnName($nmRelation->getThisProperty()); - $relIdColumn = self::buildColumnName($nmRelation->getRefProperty()); + $thisIdColumn = $otherHandler->getColumnName($nmRelation->getThisProperty(), false); + $relIdColumn = $otherHandler->getColumnName($nmRelation->getRefProperty(), false); $relEntityQuery = DatabaseEntityQuery::fetchAll($otherHandler) ->where(new CondIn(new Column($thisIdColumn), $entityIds));