WScore.DataMapper
A simple data mapper component., (*1)
Entity
Entity is a class that roughly represents a record of a database table., (*2)
The class is not pure; one needs to extend an EntityAbstract and set a modelName
to use it in this component. For example, (*3)
namespace Tasks\Entity;
class Task extends EntityAbstract
{
const STATUS_ACTIVE = '1';
const STATUS_DONE = '9';
public static $_modelName = '\App\Tasks\Model\Tasks';
public $task_id = null;
public $memo = '';
public $status = self::STATUS_ACTIVE;
}
Properties should be defined as public for PDO to populate the value., (*4)
A public static variable $_modelName should reference to the model class to handle this entity., (*5)
Models
Model class(es) determines how an entity object be
- saved to a database,
- represented in html forms,
- validated its value., (*6)
The models is consisted of several classes:, (*7)
- WScore\DataMapper\Model
a master controller model for all other models.
- WScore\DataMapper\Model\Persistence
manages how to access database layer.
- WScore\DataMapper\Model\Presentation
manages html form presentation, as well as validation.
- WScore\DataMapper\Model\Property
a hidden object to control attribute of each properties for Persistence and Presentation models.
class Tasks extends Model
{
/** @var string name of database table */
protected $table = 'task';
/** @var string name of primary key */
protected $id_name = 'task_id';
public function __construct()
{
parent::__construct();
$csv = file_get_contents( __DIR__ . '/tasks.csv' );
$this->property->prepare( $csv );
}
}
All models are PHP code., (*8)
Possible to overwrite/hack/rewrite any of the models to suite need., (*9)
Attributes of entity's properties is defined by CSV., (*10)
TODO: Presentation model simply returns information about html form and validation.
maybe model should be responsible to construct form and perform validation., (*11)
Role
A simple wrapper for entities to provide use-case specific methods called interactions., (*12)
Following shows an example to load post data into entity, validate value, and save to database., (*13)
$task = $this->em->fetch( 'Tasks\Entity\Task' );
$role = $this->role->applyDataIO( $task );
$role->load( $_POST );
if( $role->validate() ) {
$active = $this->role->applyActive( $role );
$active->save();
}
Relation
defines how entity is related to other entities., (*14)
Available relations are: HasOne, Joined, and JoinBy., (*15)
Some relation, such as isJoined, requires models to be defined., (*16)
Property Annotation
write down the following annotations in PHP or CSV., (*17)
-
column:, (*18)
database column name., (*19)
-
title:, (*20)
human readable name for the column., (*21)
-
type:, (*22)
type of the column; string, number, char (i.e. only ascii code), datetime, date, time,
created_at (i.e. datetime), updated_at (i.e. datetime), etc., (*23)
-
dbDef:, (*24)
database definition. not really used., (*25)
-
notNull:, (*26)
not null or not. not really used., (*27)
-
default:, (*28)
specify the default value used for validation.
maybe used for presentation html as default value., (*29)
-
required:, (*30)
used for validation. will be used for presentation for html form, marked as required., (*31)
-
protected:, (*32)
indicates the column will not be mass-assigned., (*33)
-
presentedAs:, (*34)
overwrites types for presentation html.
specify types such as textarea, radio, check, etc., (*35)
-
style:, (*36)
form/tags style., (*37)
-
validateAs:, (*38)
overwrites types for validation rule.
specify types such as tel, mail, etc., (*39)
-
pattern:, (*40)
regular expression, (*41)