Repository navigation
Internals 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.
use database\DatabaseManager;
use object\ObjectCollection;abstract class ModelCollection extends DatabaseManager implements \IteratorAggregate, \CountableDeclarations 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 | 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 |
public function __construct(string $table = "")| 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();
}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.
protected function createModel(?array $data = null): Model| 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;
}public function getIterator(): \TraversableComputes 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(); }public function count(): intReturn expressions (including local callbacks): count($this->obCollection->toArray()).
Implementation, validation guards and interaction order
public function count(): int { return count($this->obCollection->toArray()); }protected function getTable(): stringReturn expressions (including local callbacks): $model->inferTableName().
Implementation, validation guards and interaction order
protected function getTable(): string
{
$model = $this->createModel();
return $model->inferTableName();
}AWT Framework Wiki · Home · Installation Guide · Developer Guide · Framework Internals
Philosophy · Architecture · Known discrepancies · Verification
Installation Guide
Developer Guide
- Learning sequence
- First package
- Package structure
- Lifecycle
- Runtimes
- Events and objects
- Routing and controllers
- Views and resources
- Database and models
- Storage and caching
- Configuration and CLI
- Responses and errors
- Extension points
Framework Internals
- Architecture
- Bootstrap
- Packages
- Runtime
- Events and context
- Objects
- HTTP routing and controllers
- Rendering
- Blade integration
- Database
- Models
- Storage and caching
- Settings
- CLI
- Identity and sessions
- Responses
- Errors
- Complete source inventory
Documentation notes