-
Notifications
You must be signed in to change notification settings - Fork 0
Data Model
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.
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.
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;
}
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;
}
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';
}
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',
];
}
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 = [];
}
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'];
}
}
(c) 2025 - CodeVerve - Query Builder Library for Wordpress