MarkdownMaster CMS Logo

Database Layer

MarkdownMaster CMS provides a core database facility based on SQLite with Write-Ahead Logging (WAL) enabled. The database is only created when needed and is accessible via the Model class.

Database Path

The path to the database file is determined by: 1. The database.path setting in config.php 2. If not set, falls back to the default data/cms.sqlite relative to the site root

API Reference

Autoloading and Registration

All Models must be registered in the Model::RegisterModel() method, this is typically done in the autoload.php file.

// autoload.php
Model::RegisterModel('Popularity\Models\PopularityViewsModel');

Registering a model only registers the class in the Model handler, it does not instantiate or auto-install the database.

Model::Db()

Returns a shared PDO connection to the database for the current request. The same PDO instance is returned on subsequent calls in the same request. Returns null if the database path cannot be created or accessed.

// SomeModule/Models/MyModel.php
class MyModel extends Model {
    ...

    public static function GetSomething(): array {
        $db = self::Db();
        if ($db === null) return [];

        $stmt = $db->prepare("SELECT some_key1, some_key2
            FROM my_model
            WHERE some_param = :some_key;
        ");
        $stmt->execute(['some_key' => 'some value']);
        return $stmt->fetchAll(PDO::FETCH_ASSOC);
    }
}

Property Mapping

Each column in the mapped table must be defined as a public property in the model class. The types must match, including type, is nullable, and default value.

Each Model should only have properties which refer to the database table.

Each Model should only relate to one specific table and only perform operations against that table.

Get All Records

The Model::GetAll() method returns an array of all records in the table.

Each row in the array is an instance of the associated model.

Get Single Record

@todo / unsupported

Find Records

@todo / unsupported

Saving Records

The Model system supports upsert and insert operations.

MyModel->upsert() will perform an INSERT OR REPLACE to ensure the record exists. For upserts, all keys, including primary keys, are expected to be set.

MyModel->insert() will perform an INSERT to insert a new record. For inserts, the primary key may be omitted, notably for auto-incrementing keys.