Skip to content

Data Model

Darko Gjorgjijoski edited this page Jun 15, 2025 · 2 revisions

This package provides an abstract DataModel class that can be used to create models based on custom data tables.

The data model class is integrated with core Wordpress functions and the QueryBuilder class to provide versatility and standardization.

Model Attributes and Properties

This class is an extension of another existent DataModel class, that provides the functionality to create dynamic properties and more.

Before continuing and for the basic model usage, please read see our PHP data model documentation.

Usage

When creating a model script file, it should have the following use statements (at the beginning of the script), it should extend from the DataModel class and use the DataModelTrait, see an example:

use TenQuality\WP\Database\Abstracts\DataModel;
use TenQuality\WP\Database\Traits\DataModelTrait;

class MyModel extends DataModel
{
    use DataModelTrait;
}

Database Table Name

Define the model's table name using the protected property $table. It is recommended to add a constant as well, for more flexibility and to access this value statically (like MyModel::TABLE).

use TenQuality\WP\Database\Abstracts\DataModel;
use TenQuality\WP\Database\Traits\DataModelTrait;

class MyModel extends DataModel
{
    use DataModelTrait;
    /**
     * Data table name in database (without prefix).
     * @var string
     */
    const TABLE = 'model_table';
    /**
     * Data table name in database (without prefix).
     * @var string
     */
    protected $table = self::TABLE;
}

Primary Key

Define the model's primary key column name using the protected property $primary_key. The default is 'ID'`.

use TenQuality\WP\Database\Abstracts\DataModel;
use TenQuality\WP\Database\Traits\DataModelTrait;

class MyModel extends DataModel
{
    use DataModelTrait;
    /**
     * Data table name in database (without prefix).
     * @var string
     */
    const TABLE = 'model_table';
    /**
     * Data table name in database (without prefix).
     * @var string
     */
    protected $table = self::TABLE;
    /**
     * Primary key column name. Default ID
     * @var string
     */
    protected $primary_key = 'model_id';
}

Columns And Attributes

Define the model's attributes using the protected property `$attributes. This will be taken into consideration when inserting new records into the database.

use TenQuality\WP\Database\Abstracts\DataModel;
use TenQuality\WP\Database\Traits\DataModelTrait;

class MyModel extends DataModel
{
    use DataModelTrait;
    /**
     * Data table name in database (without prefix).
     * @var string
     */
    const TABLE = 'model_table';
    /**
     * Data table name in database (without prefix).
     * @var string
     */
    protected $table = self::TABLE;
    /**
     * Primary key column name. Default ID
     * @var string
     */
    protected $primary_key = 'model_id';
    /**
     * Model properties, data column list.
     * @var string
     */
    protected $attributes = [
        'model_id',
        'name',
    ];
}

Search keywords

Define the model's searchable columns using the protected static property $keywords.

use TenQuality\WP\Database\Abstracts\DataModel;
use TenQuality\WP\Database\Traits\DataModelTrait;

class MyModel extends DataModel
{
    use DataModelTrait;

    //    ...

    /**
     * List of properties used for keyword search.
     * @var array
     */
    protected static $keywords = [];
}

Attributes Excluded from Insert

Define the model's list of attributes/columns that should be excluded from insertion using the protected method protected_properties().

use TenQuality\WP\Database\Abstracts\DataModel;
use TenQuality\WP\Database\Traits\DataModelTrait;

class MyModel extends DataModel
{
    use DataModelTrait;

    //    ...

    /**
     * Returns list of protected/readonly properties for
     * when saving or updating.
     * 
     * @return array
     */
    protected function protected_properties()
    {
        // The following is the default array list
        return [$this->primary_key, 'created_at', 'updated_at'];
    }
}
Clone this wiki locally