Documentation

BaseClass

BaseClass provides powerful methods to allow your custom classes to use a database table. To start using the benefits, your class must extends the BaseClass:

class <Your_Class> extends BaseClass {
    ...
}

Then, all you need to do is to declare some pre-defined variables:

  • protected $attribute1
    protected $attribute2
    protected $attribute3

    An instance variable for each table attribute (EXCEPT for the ID attribute: it is automatically added)
  • protected $default_values = [
       "attribute1" => "default_value",
       "attribute2" => "default_value",
       ...
    ]

    An associative array that contains a list of table attributes with its default value (EXCEPT for the ID attribute: it is automatically added)
  • const TABLENAME = "tablename"
    A constant that have to contain the name of the table on the database

In the class constructor, you have to invoke the init method of the BaseClass and, optionally, it can accept an $id parameter to automatically load the instance with that ID:

public function __construct($id = NULL) {
    parent::init($id);
}

Optionally, you can declare some variables that are not linked with the database table. This kind of variables are excluded from every operation that involves database (insert and update).
For that, you can declare you custom variable and then the $custom_excluded instance variable that must contains all of this attributes name.
For example:

protected $myVar;  // variable not in the database table
protected $custom_excluded = ["myVar"];

Available Methods

  • fill($array)
    Fill the object with an associative array passed as parameter
  • clearAndFill($array)
    Fill the object with an associative array passed as parameter, ignoring extra attributes contained in the array
  • load($id)
    Load from the database the entry with the ID passed ad parameter
  • exist($id)
    Tell if the entry with the ID passed as paramenter exist in the database
  • save()
    Save the object in the database. If the object is already saved on the database, it will be updated. Otherwise it will be created.
    It's possible to force the ID used on the database table by passing that as an extra parameter to the method (save($id))
  • delete()
    Delete the entry (if exist) from the database.
  • lockTable()
    Lock every read and write operation on the database table until is invoked the unlockTable() method
  • unlockTable()
    Unlock the database table

BaseList

BaseList provides powerful methods to allow your custom classes to use a database table as List. To start using the benefits, your class must extends the BaseList:

class <Your_Class> extends BaseList {
    ...
}

To use BaseList you need to first declare the BaseClass table into a Constant

CONST BASE_CLASS = ::class;

Than initialize the constructor method of the BaseList class

public function __construct(){
            parent::init()
}

Available Methods

  • view($fields,$orderColumn,$orderType,$numitem,$currentPage).
    The view method can be used to obtain data from the table filtered and paginated
    view($fields, $orderColumn, $orderType, $numitem, $currentPage);

    The view method accept only this type of parameter:
    • $fields is a Array composed by three option: columnName, operator and the word to search.
    • $orderColumn contain the name of column to order by.
    • $orderType can be "ASC" or "DESC" to get data Ascended or Descended.
    • $numitem container the Limit element of how much item to view.
    • $currentPage contain the current page number according to the number of item to view.
  • loadAll()
    Load from the database table all the item and return the number of item as result.
  • truncate()
    Used for truncate the database.
  • getIterator()
    If you are used loadAll method you can use getIterator method to iterate the list like an array.
  • getItemsArray()
    Return the database table item as array.
  • size()
    Return the number of items.
  • isEmpty()
    Return boolean if the table is empty.
  • jsonSerialize()
    This method is used when json_encode() is called, it expose "items" to the json_encode() function.
  • add($element)
    This method is used to add item into array.
  • get($key)
    This method is used to get item by key.
  • remove($key,$shift)
    This method is used to remove item from array.
  • clear()
    This method is used to clear the item array.
  • fill
    This method is used to Fill the list with an array of array of object fields