Skip to content

Internals Model ModelCollection

Stefan Crkvenjakov edited this page Oct 7, 2026 · 1 revision

model\ModelCollection

Framework Internals · Subsystem overview · Developer guide

Source: awt_src/classes/model/ModelCollection.php · Snapshot 8411f4287706.

Models add hydration, persistence, relations, casts and attribute controls to DatabaseManager. Collections hydrate already fetched rows and provide iteration/counting.

Imports and dependencies

use database\DatabaseManager;
use object\ObjectCollection;

ModelCollection

Declaration and architectural role

abstract class ModelCollection extends DatabaseManager implements \IteratorAggregate, \Countable

Constants, enum cases, traits and properties

Declarations below preserve visibility, type, default and modifiers exactly. A typed property without an initializer remains uninitialized until its constructor or another method assigns it. Mutable fields belong to this instance unless marked static; readonly fields can be initialized once. Constructor-promoted properties are also listed below.

Member Declaration Purpose or source contract
$model private string $model; Used by __construct(), createModel().
$obCollection public ObjectCollection $obCollection; Used by __construct(), getIterator(), count().
$collectionTable private string $collectionTable = ''; Used by __construct(), createModel().
Constructor-promoted public function __construct(string $table = "") Initialized from the constructor argument; default expression is evaluated when the argument is omitted.

Method index

Method Signature
__construct public function __construct(string $table = "")
getModel abstract public function getModel(): string
createModel protected function createModel(?array $data = null): Model
getIterator public function getIterator(): \Traversable
count public function count(): int
getTable protected function getTable(): string

__construct

Source line 13

public function __construct(string $table = "")

Parameters

Parameter Type, modifiers and default Purpose
$table string $table = "" Operation input; the exact use, validation and propagation are shown in the implementation trace.

State writes: $this->model, $this->collectionTable, $this->obCollection.

Explicit exceptions: \InvalidArgumentException. The guards in the implementation below show when each is raised; exceptions from collaborators also propagate unless caught.

Implementation, validation guards and interaction order
public function __construct(string $table = "")
    {
        parent::__construct();

        $this->model = $this->getModel();
        if (!is_subclass_of($this->model, Model::class)) throw new \InvalidArgumentException('Collection model must extend Model.');
        if ($table === "") {
            $table = $this->getTable();
        }

        $this->collectionTable = $table;
        $results = $this->table($table)->select()->get();

        $this->obCollection = new ObjectCollection();
        $key = $this->createModel()->id_column ?? 'id';
        $this->obCollection->setKey($key)->setStrictType(Model::class);

        foreach ($results as $result) {
            $model = $this->createModel($result);
            $model->model_source = $table;
            $this->obCollection->add($model);
        }

        $this->obCollection->sortByKey();

    }

getModel

Source line 40

abstract public function getModel(): string;

Contract only: implementations supply the behavior. Parameter and return types are enforced by PHP; caller and implementation constraints follow the subsystem overview.

createModel

Source line 42

protected function createModel(?array $data = null): Model

Parameters

Parameter Type, modifiers and default Purpose
$data ?array $data = null Input data array or payload; see allowed fields and validation guards below.

Return expressions (including local callbacks): $model.

Implementation, validation guards and interaction order
protected function createModel(?array $data = null): Model
    {
        $model = (new $this->model(null))->useConnectionFrom($this);
        if ($data !== null) $model->hydrateRow($data, $this->collectionTable, $model->id_column ?? 'id');
        return $model;
    }

getIterator

Source line 50

public function getIterator(): \Traversable

Computes or delegates the operation shown below; no explicit receiver property assignment or throw occurs in this method.

Implementation, validation guards and interaction order
public function getIterator(): \Traversable { yield from $this->obCollection->toArray(); }

count

Source line 51

public function count(): int

Return expressions (including local callbacks): count($this->obCollection->toArray()).

Implementation, validation guards and interaction order
public function count(): int { return count($this->obCollection->toArray()); }

getTable

Source line 53

protected function getTable(): string

Return expressions (including local callbacks): $model->inferTableName().

Implementation, validation guards and interaction order
protected function getTable(): string
    {
        $model = $this->createModel();

        return $model->inferTableName();
    }

Related types

Clone this wiki locally