Skip to content

Commit fc230ed

Browse files
authored
Merge pull request #6293 from WoltLab/6.2-database-object-collection
Database object collections
2 parents 581df10 + 6ce33ac commit fc230ed

File tree

3 files changed

+103
-0
lines changed

3 files changed

+103
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace wcf\data;
4+
5+
/**
6+
* Abstract class for a database object that uses collections.
7+
*
8+
* @author Marcel Werk
9+
* @copyright 2001-2025 WoltLab GmbH
10+
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
11+
* @since 6.2
12+
*
13+
* @template TDatabaseObjectCollection of DatabaseObjectCollection
14+
*/
15+
abstract class CollectionDatabaseObject extends DatabaseObject
16+
{
17+
/**
18+
* @var TDatabaseObjectCollection
19+
*/
20+
protected DatabaseObjectCollection $collection;
21+
22+
/**
23+
* @param TDatabaseObjectCollection $collection
24+
*/
25+
public function setCollection(DatabaseObjectCollection $collection): void
26+
{
27+
$this->collection = $collection;
28+
}
29+
30+
/**
31+
* @return TDatabaseObjectCollection
32+
*/
33+
public function getCollection(): DatabaseObjectCollection
34+
{
35+
if (!isset($this->collection)) {
36+
$this->collection = new ($this->getCollectionClassName())([
37+
$this
38+
]);
39+
}
40+
41+
return $this->collection;
42+
}
43+
44+
public function getCollectionClassName(): string
45+
{
46+
return static::class . 'Collection';
47+
}
48+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace wcf\data;
4+
5+
/**
6+
* Abstract class for a collection of database objects.
7+
*
8+
* @author Marcel Werk
9+
* @copyright 2001-2025 WoltLab GmbH
10+
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
11+
* @since 6.2
12+
*
13+
* @template TDatabaseObject of DatabaseObject
14+
*/
15+
abstract class DatabaseObjectCollection
16+
{
17+
/**
18+
* @param TDatabaseObject[] $objects
19+
*/
20+
public function __construct(protected readonly array $objects) {}
21+
22+
/**
23+
* @return int[]
24+
*/
25+
public function getObjectIDs(): array
26+
{
27+
return \array_map(static fn($object) => $object->getObjectID(), $this->objects);
28+
}
29+
30+
/**
31+
* @return TDatabaseObject[]
32+
*/
33+
public function getObjects(): array
34+
{
35+
return $this->objects;
36+
}
37+
}

wcfsetup/install/files/lib/data/DatabaseObjectList.class.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,8 @@ public function readObjects()
212212
$this->objects = $statement->fetchObjects(($this->objectClassName ?: $this->className));
213213
}
214214

215+
$this->createCollection();
216+
215217
// decorate objects
216218
if (!empty($this->decoratorClassName)) {
217219
foreach ($this->objects as &$object) {
@@ -232,6 +234,22 @@ public function readObjects()
232234
$this->objects = $objects;
233235
}
234236

237+
/**
238+
* @since 6.2
239+
*/
240+
protected function createCollection(): void
241+
{
242+
$firstObject = \reset($this->objects);
243+
if (!($firstObject instanceof CollectionDatabaseObject)) {
244+
return;
245+
}
246+
247+
$collection = new ($firstObject->getCollectionClassName())($this->objects);
248+
foreach ($this->objects as $object) {
249+
$object->setCollection($collection);
250+
}
251+
}
252+
235253
/**
236254
* Returns the object ids of the list.
237255
*

0 commit comments

Comments
 (0)