This commit is contained in:
Roman 2023-01-12 20:55:32 +01:00
parent 83cf7d1a04
commit 84d79fcb3a
3 changed files with 28 additions and 20 deletions

@ -124,6 +124,7 @@ abstract class DatabaseEntity implements ArrayAccess, JsonSerializable {
public function preInsert(array &$row) { } public function preInsert(array &$row) { }
public function postFetch(SQL $sql, array $row) { } public function postFetch(SQL $sql, array $row) { }
public function postUpdate() { }
public static function getPredefinedValues(): array { return []; } public static function getPredefinedValues(): array { return []; }
public function postDelete() { } public function postDelete() { }

@ -578,30 +578,31 @@ class DatabaseEntityHandler implements Persistable {
$otherHandler = $nmRelation->getRelHandler(); $otherHandler = $nmRelation->getRelHandler();
$thisIdColumn = $otherHandler->getColumnName($nmRelation->getThisProperty(), false); $thisIdColumn = $otherHandler->getColumnName($nmRelation->getThisProperty(), false);
$relIdColumn = $otherHandler->getColumnName($nmRelation->getRefProperty(), false); $relIdColumn = $otherHandler->getColumnName($nmRelation->getRefProperty(), false);
if (!empty($entityIds)) {
$relEntityQuery = DatabaseEntityQuery::fetchAll($otherHandler)
->where(new CondIn(new Column($thisIdColumn), $entityIds));
$relEntityQuery = DatabaseEntityQuery::fetchAll($otherHandler) if ($recursive) {
->where(new CondIn(new Column($thisIdColumn), $entityIds)); $relEntityQuery->fetchEntities(true);
}
if ($recursive) { $rows = $relEntityQuery->executeSQL();
$relEntityQuery->fetchEntities(true); if (!is_array($rows)) {
} $this->logger->error("Error fetching n:m relations from table: '$nmTable': " . $this->sql->getLastError());
return;
}
$rows = $relEntityQuery->executeSQL(); $thisIdProperty = $otherHandler->properties[$nmRelation->getThisProperty()];
if (!is_array($rows)) { $thisIdProperty->setAccessible(true);
$this->logger->error("Error fetching n:m relations from table: '$nmTable': " . $this->sql->getLastError());
return;
}
$thisIdProperty = $otherHandler->properties[$nmRelation->getThisProperty()]; foreach ($rows as $row) {
$thisIdProperty->setAccessible(true); $relEntity = $otherHandler->entityFromRow($row, [], $recursive);
$thisEntity = $entities[$row[$thisIdColumn]];
foreach ($rows as $row) { $thisIdProperty->setValue($relEntity, $thisEntity);
$relEntity = $otherHandler->entityFromRow($row, [], $recursive); $targetArray = $property->getValue($thisEntity);
$thisEntity = $entities[$row[$thisIdColumn]]; $targetArray[$row[$relIdColumn]] = $relEntity;
$thisIdProperty->setValue($relEntity, $thisEntity); $property->setValue($thisEntity, $targetArray);
$targetArray = $property->getValue($thisEntity); }
$targetArray[$row[$relIdColumn]] = $relEntity;
$property->setValue($thisEntity, $targetArray);
} }
} else { } else {
$this->logger->error("fetchNMRelations for type '" . get_class($nmRelation) . "' is not implemented"); $this->logger->error("fetchNMRelations for type '" . get_class($nmRelation) . "' is not implemented");
@ -785,6 +786,7 @@ class DatabaseEntityHandler implements Persistable {
$res = $this->updateNM($entity, $properties); $res = $this->updateNM($entity, $properties);
} }
$entity->postUpdate();
return $res; return $res;
} }

@ -103,6 +103,11 @@ class DatabaseEntityQuery extends Select {
private function fetchRelation(string $propertyName, string $tableName, DatabaseEntityHandler $src, DatabaseEntityHandler $relationHandler, private function fetchRelation(string $propertyName, string $tableName, DatabaseEntityHandler $src, DatabaseEntityHandler $relationHandler,
int &$relIndex = 1, bool $recursive = false, string $relationColumnPrefix = "") { int &$relIndex = 1, bool $recursive = false, string $relationColumnPrefix = "") {
// TODO: fix recursion here...
if ($src === $relationHandler && $recursive) {
return;
}
$columns = $src->getColumns(); $columns = $src->getColumns();
$foreignColumn = $columns[$propertyName]; $foreignColumn = $columns[$propertyName];