Skip to content
Youmy001 edited this page Feb 4, 2018 · 13 revisions

An entity is a domain representation of an entry in a database table. The entity implements the data mapper and the factory design patterns.

The entity module is a generic data mapper flexible enough for any type of entries. It is used to fetch data from the database and keep local changes until these are saved back to the database or deleted. It allows to seperate your business logic from database abstractions. Through the Factory pattern it is possible to extend the Entity module by offering flexibility on the finding and creating procedure.

The Entity module uses the Database module. It is required to have a connection to a working database configured. It is recommended to have read about Database Access beforehand.

As a complement, please read about Lazy Loading and Entity Factories

Module Structure

Its basic member is an identifier which can be either an integer or a string depending on your database diagram. It also requires a table name in order to know where to fetch and write data. The Interface is there to standardize User Defined Entities by requiring few methods to be implemented — whose complement the data mapper design with the ability to create customized saving, deleting and loading procedures.

Basic Entity

The Basic Entity is the type of Entity that is the least complex to use. However, it is closer to a data mapper than anything else. In facts, it is a simple overlay to the data mapper class. It works directly with the data stored in the mapper which are the same as those stored in the database. It means that any of your buisness logic code will be done outside of it.

Usage Example

use Apine\Entity\BasicEntity;

$foo=new BasicEntity();
$foo->initialize("Foo");
$foo->set_field("name","Cat");
$foo->save();

$bar=new BasicEntity("Foo",$foo->get_id());
$bar->set_field("name","Dog");
$bar->delete();

Methods

__construct([string $table,[string $id]]) : null

Creates an Entity instance.

initialize(string $table,[string $id]) : null

Prepares an Entity instance for use. It is not necessary to call initialize() if you already provided a table name.

get_id() : string

Return the id of the Entity.

set_id(string $id) : null

Sets the id of the Entity.

get_field(string $field_name) : string

Returns the value of the specified field.

set_field(string $field_name,string $value) : null

Sets the value of a field.

load() : null

Loads the database entry into the entity

delete() : null

Deletes the entity from the database

save() : null

Save or update the entity into the database

User Defined Entity

The main component of the Entity Model is the User Defined Entity which represents the most efficient and flexible way to use Entity. In order to use the Entity Model your Model class must inherit from the EntityModel class and follow rules from the EntityInterface. Although its is not mandatory, we recommend to implement lazy loading into user defined entities especialy if an entity has other entities as member in order to boost performances of the application.

Example

use Apine\Entity\EntityModel;

class Foo extends EntityModel{
    private $id;
    private $name;
    
    function __construct($a_id=null){
        $this->_initialize("Foo",$a_id);
        
        $this->load();
    }
    
    function load(){
        $this->setId($this->_get_field("id"));
        $this->setName($this->_get_field("name"));
    }
    
    function save(){
        if($this->getId()==null){
            $this->_set_field("id",$this->getId());
        }
        if($this->getName()==null){
            $this->_set_field("name",$this->getName());
        }
        
        parent::_save();
        
        if($this->getId()==null){
            $this->set_id($this->_get_id());
        }
    }
    
    function delete(){
        parent::_destroy();
    }
    
    function getId(){
        return $this->id;
    }
    
    function getName(){
        return $this->name;
    }
    
    function setId($a_id){
        $this->id=$a_id;
    }
    
    function setName($a_name){
        $this->name=$a_name;
    }
}
    

$foo=new Foo();
$foo->setName("Cat");
$foo->save();

$bar=new Foo($foo->getId());
$bar->setName("Dog");
$bar->delete();

Mandatory Inherited Abstract Methods

EntityInterface::load()

Load data into entity

EntityInterface::save()

Save entity

EntityInterface::delete()

Delete entity

Clone this wiki locally