ORM Library class to create or load an object representing a database table row (i.e. User, Contract, etc.).
@requires CorbeauPerdu\Database\DBWrapper
See: ORMUsageExamples.php
To create a new object template, use User.php as a starting point. The following methods needs to be called (in order!) and modified for your needs in the constructor:
- $this->setTablename() : sets the database tablename
- $this->addMember() : sets the column's properties (column name, type, default value, etc.)
- $this->setPrimaryKey() : sets the table's primary key column name (use the friendly key name given in addMember!)
- $this->setUseUpsert() : force an UPDATE if an INSERT fails due to DUPLICATE KEY? In other words, does an UPSERT (Optional! Defaults to FALSE).
- parent::__construct() : validates object is ready; also sets DBWrapper's DEBUG mode and its APPNAME for log outputs
That's all you need! You can now use your Object class.
Object Methods:
- Object's constructor : either creates a new object with default values, or loads an object from the database.
- $object->save() : save the loaded object into the database.
- $object->delete() : delete the loaded object from the database.
- $object->isDirty() : is the object new, or as been modified?
- $object->isLoaded() : is the object loaded from or saved into the DB?
- $object->isNew() : is the object in NEW state?
- $object->isDeleted() : is the object in a DELETED (from DB) state?
- $object->getEntity() : retrieve the entity object that holds the data (for debug purposes!)
- $object->propertyName : set or get propertyName's column value!
Static Methods:
- Object::find() : find and load objects from the database.
- Object::deleteQuery() : delete objects from the database.
- Object::getDBWrapper(): retrieve a DBWrapper object
Thrown ORMExceptions listing:
- Code 1 = Property isn't defined in the entity!
- Code 2 = Property is readonly!
- Code 3 = Tablename, primary key or member properties not properly set!
- Code 4 = Specified primary key isn't set in the entity!
- Code 5 = Wrong parameter type for filter!
- Code 6 = More than 1 row returned! Cannot initialize the object with many rows!
- Code 7 = Save aborted: the object was previously deleted from DB!
- Code 8 = Missing value for property!
- Code 9 = Rows might have been saved in the DB, but DB returned warnings (only if using MySQL for now!)
- Code 10 = Wrong number of rows updated in the DB after save()!
- Code 11 = Wrong number of rows removed in the DB after delete()!
- Code 12 = Can't delete an object that hasn't yet been saved in the database!
- Code 13 = Can't delete an object that doesn't have a value set in the primary key!
- Code 14 = Invalid datetime value for property
- Code 15 = Invalid value datatype for property
- Code 16 = There is no property with the given DB column name!
Trick: The constructor(), save(), find() and deleteQuery() all accept a $forceCloseDB and $dbwrapper reference parameter! This allows you to either force closing the DB connection after each query, and it'll also allow you to use the same DBWrapper / PDO Connection throughout your page if desired.
i.e. say you manipulate a User object, then load a Contract object, why have Contract use a different DBWrapper?
Initialize a DBWRapper with $mydb = Object::getDBWrapper()
and pass it along to all objects!