predefined entities + override database entity handler
This commit is contained in:
@@ -112,6 +112,7 @@ abstract class DatabaseEntity implements ArrayAccess, JsonSerializable {
|
||||
|
||||
public function preInsert(array &$row) { }
|
||||
public function postFetch(SQL $sql, array $row) { }
|
||||
public static function getPredefinedValues(SQL $sql): array { return []; }
|
||||
|
||||
public static function fromRow(SQL $sql, array $row): static {
|
||||
$handler = self::getHandler($sql);
|
||||
@@ -201,7 +202,7 @@ abstract class DatabaseEntity implements ArrayAccess, JsonSerializable {
|
||||
return false;
|
||||
}
|
||||
|
||||
public static function getHandler(SQL $sql, $obj_or_class = null): DatabaseEntityHandler {
|
||||
public static function getHandler(SQL $sql, $obj_or_class = null, $allowOverride = false): DatabaseEntityHandler {
|
||||
|
||||
if (!$obj_or_class) {
|
||||
$obj_or_class = get_called_class();
|
||||
@@ -213,14 +214,16 @@ abstract class DatabaseEntity implements ArrayAccess, JsonSerializable {
|
||||
$class = $obj_or_class;
|
||||
}
|
||||
|
||||
// if we are in an extending context, get the database handler for the root entity,
|
||||
// as we do not persist attributes of the inheriting class
|
||||
while ($class->getParentClass()->getName() !== DatabaseEntity::class) {
|
||||
$class = $class->getParentClass();
|
||||
if (!$allowOverride) {
|
||||
// if we are in an extending context, get the database handler for the root entity,
|
||||
// as we do not persist attributes of the inheriting class
|
||||
while ($class->getParentClass()->getName() !== DatabaseEntity::class) {
|
||||
$class = $class->getParentClass();
|
||||
}
|
||||
}
|
||||
|
||||
$handler = self::$handlers[$class->getShortName()] ?? null;
|
||||
if (!$handler) {
|
||||
if (!$handler || $allowOverride) {
|
||||
$handler = new DatabaseEntityHandler($sql, $class);
|
||||
self::$handlers[$class->getShortName()] = $handler;
|
||||
}
|
||||
|
||||
@@ -468,7 +468,8 @@ class DatabaseEntityHandler implements Persistable {
|
||||
$property->setAccessible(true);
|
||||
$relEntities = $property->getValue($entity);
|
||||
foreach ($relEntities as $relEntity) {
|
||||
$nmRow = [$entity->getId(), $relEntity->getId()];
|
||||
$relEntityId = (is_int($relEntity) ? $relEntity : $relEntity->getId());
|
||||
$nmRow = [$entity->getId(), $relEntityId];
|
||||
if (!empty($dataColumns)) {
|
||||
foreach (array_keys($dataColumns[$thisTableName]) as $propertyName) {
|
||||
$nmRow[] = $property->getName() === $propertyName;
|
||||
@@ -621,9 +622,20 @@ class DatabaseEntityHandler implements Persistable {
|
||||
public function getCreateQueries(SQL $sql): array {
|
||||
|
||||
$queries = [];
|
||||
$table = $this->getTableName();
|
||||
|
||||
// Create Table
|
||||
$queries[] = $this->getTableQuery($sql);
|
||||
|
||||
$table = $this->getTableName();
|
||||
// pre defined values
|
||||
$getPredefinedValues = $this->entityClass->getMethod("getPredefinedValues");
|
||||
$getPredefinedValues->setAccessible(true);
|
||||
$predefinedValues = $getPredefinedValues->invoke(null, $sql);
|
||||
if ($predefinedValues) {
|
||||
$queries[] = $this->getInsertQuery($predefinedValues);
|
||||
}
|
||||
|
||||
// Entity Log
|
||||
$entityLogConfig = $this->entityClass->getProperty("entityLogConfig");
|
||||
$entityLogConfig->setAccessible(true);
|
||||
$entityLogConfig = $entityLogConfig->getValue();
|
||||
|
||||
Reference in New Issue
Block a user