Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions wcfsetup/install/files/lib/data/CollectionDatabaseObject.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace wcf\data;

/**
* Abstract class for a database object that uses collections.
*
* @author Marcel Werk
* @copyright 2001-2025 WoltLab GmbH
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
* @since 6.2
*
* @template TDatabaseObjectCollection of DatabaseObjectCollection
*/
abstract class CollectionDatabaseObject extends DatabaseObject
{
/**
* @var TDatabaseObjectCollection
*/
protected DatabaseObjectCollection $collection;

/**
* @param TDatabaseObjectCollection $collection
*/
public function setCollection(DatabaseObjectCollection $collection): void
{
$this->collection = $collection;
}

/**
* @return TDatabaseObjectCollection
*/
public function getCollection(): DatabaseObjectCollection
{
if (!isset($this->collection)) {
$this->collection = new ($this->getCollectionClassName())([
$this
]);
}

return $this->collection;
}

public function getCollectionClassName(): string
{
return static::class . 'Collection';
}
}
37 changes: 37 additions & 0 deletions wcfsetup/install/files/lib/data/DatabaseObjectCollection.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace wcf\data;

/**
* Abstract class for a collection of database objects.
*
* @author Marcel Werk
* @copyright 2001-2025 WoltLab GmbH
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
* @since 6.2
*
* @template TDatabaseObject of DatabaseObject
*/
abstract class DatabaseObjectCollection
{
/**
* @param TDatabaseObject[] $objects
*/
public function __construct(protected readonly array $objects) {}

/**
* @return int[]
*/
public function getObjectIDs(): array
{
return \array_map(static fn($object) => $object->getObjectID(), $this->objects);
}

/**
* @return TDatabaseObject[]
*/
public function getObjects(): array
{
return $this->objects;
}
}
18 changes: 18 additions & 0 deletions wcfsetup/install/files/lib/data/DatabaseObjectList.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,8 @@ public function readObjects()
$this->objects = $statement->fetchObjects(($this->objectClassName ?: $this->className));
}

$this->createCollection();

// decorate objects
if (!empty($this->decoratorClassName)) {
foreach ($this->objects as &$object) {
Expand All @@ -232,6 +234,22 @@ public function readObjects()
$this->objects = $objects;
}

/**
* @since 6.2
*/
protected function createCollection(): void
{
$firstObject = \reset($this->objects);
if (!($firstObject instanceof CollectionDatabaseObject)) {
return;
}

$collection = new ($firstObject->getCollectionClassName())($this->objects);
foreach ($this->objects as $object) {
$object->setCollection($collection);
}
}

/**
* Returns the object ids of the list.
*
Expand Down