-
Notifications
You must be signed in to change notification settings - Fork 1
Entity Model
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
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.
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.
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();
Creates an Entity instance.
Prepares an Entity instance for use. It is not necessary to call initialize()
if you already provided a table name.
Return the id of the Entity.
Sets the id of the Entity.
Returns the value of the specified field.
Sets the value of a field.
Loads the database entry into the entity
Deletes the entity from the database
Save or update the entity into the database
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.
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();
Load data into entity
Save entity
Delete entity
- Introduction
- Getting Started
- Requirements & Installation
- Application Configuration
- Database Connection
- How to use Controllers
- How to use Views
- How to Manage Session
- How to use the api (Coming Soon)
- Utilities
- Entity Manager
- File Manager
- Access and Save File (Coming Soon)
- Image Methods (Coming Soon)
- XML Manager
- XML Class (Coming Soon)
- Parser (Coming Soon)
- Writer (Coming Soon)
- Advanced Topics
- Extended Users
- Routes
- Templating (Coming Soon)
- Translations and Locales