nm bugfix + pagination default order params
This commit is contained in:
parent
13f7866d42
commit
dee9d8c34e
@ -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']),
|
||||
];
|
||||
}
|
||||
|
||||
|
@ -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 {
|
||||
|
||||
|
@ -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 {
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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));
|
||||
|
Loading…
Reference in New Issue
Block a user