Skip to content

Database object collections#6293

Merged
BurntimeX merged 2 commits into6.2from
6.2-database-object-collection
May 14, 2025
Merged

Database object collections#6293
BurntimeX merged 2 commits into6.2from
6.2-database-object-collection

Conversation

@BurntimeX
Copy link
Member

@BurntimeX BurntimeX commented May 14, 2025

The idea behind database object collections is to lazy-load additional data when it is needed, abandoning the concept of the "viewable" decorators for database objects.

Example:

class FoobarCollection extends DatabaseObjectCollection
{
    /**
     * @var Label[][]
     */
    private array $labels;

    /**
     * @return Label[]
     */
    public function getLabels(Foobar $object): array
    {
        $this->loadLabels();

        return $this->labels[$object->getObjectID()] ?? [];
    }

    private function loadLabels(): void
    {
        if (isset($this->labels)) {
            return;
        }

        $this->labels = LabelObjectHandler::getInstance()->getAssignedLabels(
            \array_map(
                static fn($object) => $object->getObjectID(),
                \array_filter($this->objects, static fn($object) => $object->hasLabels)
            )
        );
    }
}

class Foobar extends CollectionDatabaseObject {
    /**
     * @return Label[]
     */
    public function getLabels(): array
    {
        return $this->getCollection()->getLabels($this);
    }
}

@BurntimeX BurntimeX requested a review from dtdesign May 14, 2025 13:17
@dtdesign dtdesign removed their request for review May 14, 2025 13:59
@BurntimeX BurntimeX merged commit fc230ed into 6.2 May 14, 2025
7 checks passed
@BurntimeX BurntimeX deleted the 6.2-database-object-collection branch May 14, 2025 14:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant