Versions Compared


  • This line was added.
  • This line was removed.
  • Formatting was changed.

Crud mapper concepts

CRUD mapper is a trait that designed to simplify loading and saving data to database. It might look similar to data_object, however it has substantial differences and idea behind it:

  • crud_mapper is not intended to reinvent the Active Record. AR has many disadvantages, like reduced testability, and more important mixing domain logic with DB operations.
  • crud_mapper solo purpose is to reduce verbosity of loading and saving data, while taking some safety measures during mapping/unmapping of record stdClass to crud class and allow to easily replace it if needed.
  • crud_mapper uses composition instead of inheritance allowing Domain Driven Design implementations of logic
  • crud_mapper require less definitions related to DB setting convention over configuration
  • crud_mapper can be used for creating instances from webapi calls and provide consistent results with DB.
  • In crud_mapper all columns are required and automatically checked from database table definition.
  • crud_mapper does not implement save(), load(), or delete() methods to allow changing implementation when required without changing API and add domain logic in this methods prior saving/deletion or loading.

How to use crud_mapper

  1. Add "use mod_facetoface\traits\crud_mapper" to class definition.
  2. Add const DBTABLE to class definition with table name.
  3. Make sure that all fields that are in database exist in class.
  4. Implement public function save() that optionally make sure that data are correct to be saved and call $this→crud_save();
  5. Implement public function load() that will call $this→crud_load() and optionally prepare some other data.


titleExample crud enabled class implementation

Instantiation of crud mapped classes

All crud enabled classes are expected to have public function load(int $id) which will load data from table.