Skip to content

ORM library to create objects out of database table rows.

License

Notifications You must be signed in to change notification settings

ravenlost/PHP_ORM

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CorbeauPerdu\ORM\ORMBase Class

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

Defining an object's definition template:

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:

  1. $this->setTablename() : sets the database tablename
  2. $this->addMember() : sets the column's properties (column name, type, default value, etc.)
  3. $this->setPrimaryKey() : sets the table's primary key column name (use the friendly key name given in addMember!)
  4. $this->setUseUpsert() : force an UPDATE if an INSERT fails due to DUPLICATE KEY? In other words, does an UPSERT (Optional! Defaults to FALSE).
  5. 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.

Using an object:

Object Methods:

  1. Object's constructor : either creates a new object with default values, or loads an object from the database.
  2. $object->save() : save the loaded object into the database.
  3. $object->delete() : delete the loaded object from the database.
  4. $object->isDirty() : is the object new, or as been modified?
  5. $object->isLoaded() : is the object loaded from or saved into the DB?
  6. $object->isNew() : is the object in NEW state?
  7. $object->isDeleted() : is the object in a DELETED (from DB) state?
  8. $object->getEntity() : retrieve the entity object that holds the data (for debug purposes!)
  9. $object->propertyName : set or get propertyName's column value!

Static Methods:

  1. Object::find() : find and load objects from the database.
  2. Object::deleteQuery() : delete objects from the database.
  3. Object::getDBWrapper(): retrieve a DBWrapper object

Thrown ORMExceptions listing:

  1. Code 1 = Property isn't defined in the entity!
  2. Code 2 = Property is readonly!
  3. Code 3 = Tablename, primary key or member properties not properly set!
  4. Code 4 = Specified primary key isn't set in the entity!
  5. Code 5 = Wrong parameter type for filter!
  6. Code 6 = More than 1 row returned! Cannot initialize the object with many rows!
  7. Code 7 = Save aborted: the object was previously deleted from DB!
  8. Code 8 = Missing value for property!
  9. Code 9 = Rows might have been saved in the DB, but DB returned warnings (only if using MySQL for now!)
  10. Code 10 = Wrong number of rows updated in the DB after save()!
  11. Code 11 = Wrong number of rows removed in the DB after delete()!
  12. Code 12 = Can't delete an object that hasn't yet been saved in the database!
  13. Code 13 = Can't delete an object that doesn't have a value set in the primary key!
  14. Code 14 = Invalid datetime value for property
  15. Code 15 = Invalid value datatype for property
  16. 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!

About

ORM library to create objects out of database table rows.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages