PHP Classes


Recommend this page to a friend!
  Classes of Bartlomiej Rudzki  >  EVDB  >  README  >  Download  
Role: Documentation
Content type: text/plain
Description: Readme file
Class: EVDB
Store and retrieve objects in MongoDB databases
Author: By
Last change:
Date: 7 years ago
Size: 4,027 bytes


Class file image Download
Key features of EVDB:

You can define "classes" or "types" of objects which determine:
  - a collection where the object of a given class is stored in MongoDB (like a table in MySQL)
  - a default set of properties
  name: string(Default Name)
  age: integer(18)

A property can be either of a built-in type (boolean, string, integer, float; with or without the default value) or any other previously defined type (as an embedded property or a reference pointer)
  city: string
  street: string
  house_number: string
  name: string
  address: *Address
  login: string
  password: string
  address: *Address # stored inside the User object, independent from any other Address objects
  favourite_place: &Place # reference pointer to the actual Place object

Arrays can be of defined or undefined length
  movies: string[]
  top_3_awards: string[3]

A type can inherit from 0 or more previously defined types
%Person:         # "%" defines a class as abstract, see below
  name: string(Default Name)
Actor < Person:
  movies: string[]
Bodybuilder < Person:
  lift_weight: float(123.4)
Politician < Person:
  party: string
  elected_on: integer

# The fun part
ArnoldSchwarzenegger < [Actor, Bodybuilder, Politician]:
  name: string(!!Arnold Schwarzenegger)     # "!!" defines the casting rule, see below

So, an object of ArnoldSchwarzenegger type will have the following default properties:
  - name (string, "Arnold Schwarzenegger" value by default)
  - movies (array)
  - lift_weight (float, 123.4 value by default)
  - party (string)
  - elected_on (integer)

and will be stored in the "ArnoldSchwarzenegger" collection.

Creating a new object is really simple:
$actor = EVDB::create('Actor');

Objects casting into another type can be done by writing something like:

$actor = EVDB::create('Actor'); // see Actor type in the example above
$politican = $actor->cast('Politician'); // it will have all default Politician properties added (and overwritten, if defined in the casting rules, see the name property of ArnoldSchwarzenegger type above for an example of the overwriting casting rule)

Object after casting will be saved in the collection of its new type.

A class can be defined as abstract, meaning that it only acts as a property set definition. An object of the abstract type cannot be created nor can an object be cast into the abstract type.

Plugins for specific EVDB object actions can be added
[Example] (EVDBUserPlugin <- customizes the "User" type)
// example of an event listener

public static function onBeforeSave(EVDBObject $obj)
   self::ensureUnique($obj, 'login'); // doesn't allow storing the User object with the same value of "login" property
   $obj->password = self::encode($obj->password); // to prevent storing the open-text passwords in the database
   return TRUE; // if omitted, the object will not be saved!

// example of a "magic" function
public static function getName($obj)
  return $obj->first_name.' '.$obj->last_name;

Collection/type plugins are automatically loaded by the EVDB, so to extend the functionality of a specific type defined in the EVDB schema, you just add a new file into the specific plugin directory (lib/DB/plugins) and the magic kicks in ;)

Queries may be performed using some more "magic" methods
$politicians = EVDB::find5PoliticiansByParty('Democrats'); // returns an array of up to 5 "Politician" objects with the property party equals to "Democrats";

Note: None of the "magic" methods is specified in the code, but if there's a "Politician" type with a "party" property, the methods like EVDB::findPoliticianByParty(), EVDB::findAllPoliticiansByParty(), EVDB::find123PoliticiansByParty(), etc. all can be used in the code.