From e4e2511d1c50a001016019d093a1e7ec0c01833a Mon Sep 17 00:00:00 2001 From: Roman Date: Sun, 22 Jan 2023 12:44:03 +0100 Subject: [PATCH] NoFetch Attribute --- Core/Objects/DatabaseEntity/Attribute/NoFetch.class.php | 7 +++++++ .../DatabaseEntity/Controller/DatabaseEntityHandler.php | 7 ++++++- .../Controller/DatabaseEntityQuery.class.php | 8 +++++++- 3 files changed, 20 insertions(+), 2 deletions(-) create mode 100644 Core/Objects/DatabaseEntity/Attribute/NoFetch.class.php diff --git a/Core/Objects/DatabaseEntity/Attribute/NoFetch.class.php b/Core/Objects/DatabaseEntity/Attribute/NoFetch.class.php new file mode 100644 index 0000000..90fdce5 --- /dev/null +++ b/Core/Objects/DatabaseEntity/Attribute/NoFetch.class.php @@ -0,0 +1,7 @@ +relations[$propertyName])) { + $property = $this->properties[$propertyName]; $relationHandler = $this->relations[$propertyName]; $relColumnPrefix = self::buildColumnName($propertyName) . "_"; if (array_key_exists($relColumnPrefix . "id", $row)) { $relId = $row[$relColumnPrefix . "id"]; if ($relId !== null) { - if ($fetchEntities !== DatabaseEntityQuery::FETCH_NONE) { + if ($fetchEntities !== DatabaseEntityQuery::FETCH_NONE && !self::getAttribute($property, NoFetch::class)) { if ($this === $relationHandler) { if ($context) { @@ -591,6 +593,9 @@ class DatabaseEntityHandler implements Persistable { $nmTable = $nmRelation->getTableName(); $property = $this->properties[$nmProperty]; $property->setAccessible(true); + if (self::getAttribute($property, NoFetch::class)) { + continue; + } if ($nmRelation instanceof NMRelation) { $thisIdColumn = $nmRelation->getIdColumn($this); diff --git a/Core/Objects/DatabaseEntity/Controller/DatabaseEntityQuery.class.php b/Core/Objects/DatabaseEntity/Controller/DatabaseEntityQuery.class.php index e6e27be..eebbf33 100644 --- a/Core/Objects/DatabaseEntity/Controller/DatabaseEntityQuery.class.php +++ b/Core/Objects/DatabaseEntity/Controller/DatabaseEntityQuery.class.php @@ -7,6 +7,7 @@ use Core\Driver\SQL\Column\Column; use Core\Driver\SQL\Expression\Alias; use Core\Driver\SQL\Query\Select; use Core\Driver\SQL\SQL; +use Core\Objects\DatabaseEntity\Attribute\NoFetch; /** * this class is similar to \Driver\SQL\Query\Select but with reduced functionality @@ -92,7 +93,6 @@ class DatabaseEntityQuery extends Select { return new DatabaseEntityQuery($handler, SQL::FETCH_ONE); } - // TODO: clean this up public function fetchEntities(bool $recursive = false): DatabaseEntityQuery { $this->fetchSubEntities = ($recursive ? self::FETCH_RECURSIVE : self::FETCH_DIRECT); @@ -108,6 +108,12 @@ class DatabaseEntityQuery extends Select { private function fetchRelation(string $propertyName, string $tableName, DatabaseEntityHandler $src, DatabaseEntityHandler $relationHandler, bool $recursive = false, string $relationColumnPrefix = "", array &$visited = []) { + + $property = $src->getProperty($propertyName); + if (DatabaseEntityHandler::getAttribute($property, NoFetch::class)) { + return; + } + $relIndex = count($visited); if (in_array($relationHandler->getTableName(), $visited)) { return;