nm bugfix + pagination default order params

This commit is contained in:
Roman 2023-01-11 13:30:08 +01:00
parent 13f7866d42
commit dee9d8c34e
5 changed files with 43 additions and 17 deletions

@ -12,12 +12,12 @@ use Core\Objects\DatabaseEntity\User;
trait Pagination { trait Pagination {
static function getPaginationParameters(array $orderColumns): array { static function getPaginationParameters(array $orderColumns, string $defaultOrderBy = "id", string $defaultSortOrder = "asc"): array {
return [ return [
'page' => new Parameter('page', Parameter::TYPE_INT, true, 1), 'page' => new Parameter('page', Parameter::TYPE_INT, true, 1),
'count' => new Parameter('count', Parameter::TYPE_INT, true, 20), 'count' => new Parameter('count', Parameter::TYPE_INT, true, 20),
'orderBy' => new StringType('orderBy', -1, true, "id", $orderColumns), 'orderBy' => new StringType('orderBy', -1, true, $defaultOrderBy, $orderColumns),
'sortOrder' => new StringType('sortOrder', -1, true, 'asc', ['asc', 'desc']), 'sortOrder' => new StringType('sortOrder', -1, true, $defaultSortOrder, ['asc', 'desc']),
]; ];
} }

@ -2,6 +2,9 @@
namespace Core\Objects\DatabaseEntity\Attribute; 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)] #[\Attribute(\Attribute::TARGET_PROPERTY)]
class Multiple { class Multiple {

@ -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
namespace Core\Objects\DatabaseEntity\Attribute; // 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)] #[\Attribute(\Attribute::TARGET_PROPERTY)]
class MultipleReference { class MultipleReference {

@ -22,7 +22,7 @@ abstract class DatabaseEntity implements ArrayAccess, JsonSerializable {
private static array $handlers = []; private static array $handlers = [];
protected ?int $id; protected ?int $id;
#[Transient] protected array $customData = []; #[Transient] public array $customData = [];
public function __construct(?int $id = null) { public function __construct(?int $id = null) {
$this->id = $id; $this->id = $id;
@ -101,6 +101,10 @@ abstract class DatabaseEntity implements ArrayAccess, JsonSerializable {
} }
} }
if ($propertyNames === null) {
$jsonArray = array_merge($jsonArray, $this->customData);
}
return $jsonArray; return $jsonArray;
} }

@ -149,7 +149,12 @@ class DatabaseEntityHandler implements Persistable {
} }
} }
} else if ($propertyTypeName === 'int') { } else if ($propertyTypeName === 'int') {
$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); $this->columns[$propertyName] = new IntColumn($columnName, $nullable, $defaultValue);
}
} else if ($propertyTypeName === 'float') { } else if ($propertyTypeName === 'float') {
$this->columns[$propertyName] = new FloatColumn($columnName, $nullable, $defaultValue); $this->columns[$propertyName] = new FloatColumn($columnName, $nullable, $defaultValue);
} else if ($propertyTypeName === 'double') { } else if ($propertyTypeName === 'double') {
@ -259,12 +264,20 @@ class DatabaseEntityHandler implements Persistable {
return $this->relations; return $this->relations;
} }
public function getColumnName(string $property): string { public function getColumnName(string $property, bool $withTableName = true): string {
if ($withTableName) {
if ($property === "id") { if ($property === "id") {
return "$this->tableName.id"; return "$this->tableName.id";
} else { } else {
return $this->tableName . "." . $this->columns[$property]->getName(); return $this->tableName . "." . $this->columns[$property]->getName();
} }
} else {
if ($property === "id") {
return "id";
} else {
return $this->columns[$property]->getName();
}
}
} }
public function getColumnNames(): array { public function getColumnNames(): array {
@ -295,6 +308,10 @@ class DatabaseEntityHandler implements Persistable {
return $this->nmRelations[$property]; return $this->nmRelations[$property];
} }
public function getProperty(string $property): \ReflectionProperty {
return $this->properties[$property];
}
public static function getPrefixedRow(array $row, string $prefix): array { public static function getPrefixedRow(array $row, string $prefix): array {
$rel_row = []; $rel_row = [];
foreach ($row as $relKey => $relValue) { foreach ($row as $relKey => $relValue) {
@ -407,8 +424,9 @@ class DatabaseEntityHandler implements Persistable {
$otherHandler = $nmRelation->getOtherHandler($this); $otherHandler = $nmRelation->getOtherHandler($this);
$refIdColumn = $nmRelation->getIdColumn($otherHandler); $refIdColumn = $nmRelation->getIdColumn($otherHandler);
} else if ($nmRelation instanceof NMRelationReference) { } else if ($nmRelation instanceof NMRelationReference) {
$thisIdColumn = self::buildColumnName($nmRelation->getThisProperty()); $otherHandler = $nmRelation->getRelHandler();
$refIdColumn = self::buildColumnName($nmRelation->getRefProperty()); $thisIdColumn = $otherHandler->getColumnName($nmRelation->getThisProperty(), false);
$refIdColumn = $otherHandler->getColumnName($nmRelation->getRefProperty(), false);
} else { } else {
throw new \Exception("updateNM not implemented for type: " . get_class($nmRelation)); throw new \Exception("updateNM not implemented for type: " . get_class($nmRelation));
} }
@ -449,8 +467,9 @@ class DatabaseEntityHandler implements Persistable {
$thisIdColumn = $nmRelation->getIdColumn($this); $thisIdColumn = $nmRelation->getIdColumn($this);
$refIdColumn = $nmRelation->getIdColumn($otherHandler); $refIdColumn = $nmRelation->getIdColumn($otherHandler);
} else if ($nmRelation instanceof NMRelationReference) { } else if ($nmRelation instanceof NMRelationReference) {
$thisIdColumn = self::buildColumnName($nmRelation->getThisProperty()); $otherHandler = $nmRelation->getRelHandler();
$refIdColumn = self::buildColumnName($nmRelation->getRefProperty()); $thisIdColumn = $otherHandler->getColumnName($nmRelation->getThisProperty(), false);
$refIdColumn = $otherHandler->getColumnName($nmRelation->getRefProperty(), false);
} else { } else {
throw new \Exception("insertNM not implemented for type: " . get_class($nmRelation)); throw new \Exception("insertNM not implemented for type: " . get_class($nmRelation));
} }
@ -550,8 +569,8 @@ class DatabaseEntityHandler implements Persistable {
} }
} else if ($nmRelation instanceof NMRelationReference) { } else if ($nmRelation instanceof NMRelationReference) {
$otherHandler = $nmRelation->getRelHandler(); $otherHandler = $nmRelation->getRelHandler();
$thisIdColumn = self::buildColumnName($nmRelation->getThisProperty()); $thisIdColumn = $otherHandler->getColumnName($nmRelation->getThisProperty(), false);
$relIdColumn = self::buildColumnName($nmRelation->getRefProperty()); $relIdColumn = $otherHandler->getColumnName($nmRelation->getRefProperty(), false);
$relEntityQuery = DatabaseEntityQuery::fetchAll($otherHandler) $relEntityQuery = DatabaseEntityQuery::fetchAll($otherHandler)
->where(new CondIn(new Column($thisIdColumn), $entityIds)); ->where(new CondIn(new Column($thisIdColumn), $entityIds));