The Entity Framework

Blue ERP is built around a CRUD framework called entity, which allows for easy and quick creation of complex objects (called 'entity') and relationships between them (including compositions of entities and relationships 'one to many' and 'many to many' between entities).
Content:

Objectives
Provide an easy set of classes that would allow to represent and manipulate complex object/entities stored in a database backend. Envisaged manipulation are:

  • browse and simple search
  • advanced search (*)
  • view
  • edit
  • delete
  • copy/save as
  • audit log (*)
  • ACL (*)

 
(*) not yet implemented
Custom actions on entities can then be added through extending the base classes provided.

Example of entities used in Blue ERP
Simple entity: a tax, which is defined by a number of fields
More complex entity: an invoice, which has its own data and has one or many invoice items (or invoice rows) associated to it.
Invoice items are called 'sub entities' of the invoice entity, and in this case there is a one to many relationship between the two.
More complex entity: a product. Beyond its own data fields, a product is associated with to an arbitrary number of tax. Reversely, a given tax can be associated to more than one product (and in fact it is even associated with other type of entities). This illustrates the many to many relationship. In this case, the tax entity is defined as being an indirect sub entity (indirect because in effect a table exists in the database that associates product ids with tax ids)
 
Base class of the Entity Framework
Class Field
Fields represent data that an entity has and that can be edited/shown to the end user. A Field is responsible for converting a given value between the database format, an internal representation of the value and a user displayable format.

The Field class is created using the CreateField function rather than the 'new' operator. This allows to benefit from the additional constructor methods which qualify the field further with additional information. This uses the ability of php5 to call a method on an object which is returned by a function (this is not possible in php4). All entity classes use this feature as a way to specify an optional default argument by using the set_default method. For instance consider the following line of code:
CreateField('DateField', 'Date', 'object_date')-> setDefault(strftime('%Y/%m/%d'));
This will create a field of class DateField, and specify that the default value for this field is the result of the function call strftime('%Y/%m/%d') (i.e. today's date in y-m-d format)

The base Field class has a number of subclasses, each corresponding to its own specialisation. The current hierarchy is as follows:

  • Field
    • SelectField
      • ForeignkeyField (deprecated)
      • RelatedEntityField
    • CheckboxField
    • BooleanField
    • LabelField
    • HiddenField
    • NumberField
    • DateField
    • TextareaField

Class Entity
Two type of responsibilities (maybe should be split into two classes):
Meta responsibility:

  • Responsible for defining its fields and sub entities.
  • Responsible for knowing how to display itself when beign browsed, searched, looked for
  • Responsible for knowing which classes are involved in the MVC model

Entity object responsibility:

  • Responsible for re-processing data when read to and from the database
  • Responsible for default values of the entity when a new one is created
  • Responsible for validating entity data is consistent

Back to top