Documentation

Models

BaseClass

Boostack has its own way of instantiating model objects that allows for complete instance management and interaction with the database.

The Boostack standard involves extending the BaseClass or BaseClassTraced class in a new file, which should be placed inside the my/Models folder.

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

namespace My\Models;
class <Your_Class> extends \Boostack\Models\BaseClass {
    protected $attribute1
    protected $attribute2
    protected $attribute3
        ...

    protected $default_values = [
        "attribute1" => "default_value",
        "attribute2" => "default_value",
        ...
    ]

    const TABLENAME = "tablename"; //name of the database table

    /**
    * Constructor.
    *
    * @param mixed|null $id The ID of the object.
    */
    public function __construct($id = NULL) {
            parent::init($id);
    }
}

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"];

BaseClassTraced

BaseClassTraced extends the functionality of BaseClass by including complete management of the created_at, last_update, last_access and deleted_at events.

Updating these fields within the database is handled automatically by BaseClassTraced, so you don't have to worry about anything.

The only requirement is that the database table must have the following columns:

ALTER TABLE `[tablename]` 
ADD `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
ADD `last_update` timestamp NOT NULL DEFAULT current_timestamp(),
ADD `last_access` timestamp NOT NULL DEFAULT current_timestamp(),
ADD `deleted_at` timestamp NULL DEFAULT NULL

Soft Delete

Boostack allows for automatic management of the soft delete procedure. It's sufficient to include the command
$this->soft_delete = true; in the constructor.

This way, when the `delete()` method is used to remove the object, it will not be deleted from the database but its 'deleted_at' field will be set to the value of the current timestamp.
By default, the soft_delete for objects that extend the BaseClass is set to FALSE.

/**
* Constructor.
*
* @param mixed|null $id The ID of the object.
*/
public function __construct($id = null)
{
    parent::init($id);
    $this->soft_delete = true;
}
                

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
  • static 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. If soft_delete = true call softDelete() otherwise call purge() to permanently delete from database.
  • restore()
    Restore object from database only if soft_delete = true.
  • lockTable()
    Lock every read and write operation on the database table until is invoked the unlockTable() method
  • unlockTable()
    Unlock the database table.
  • jsonSerialize()
    This method is used when json_encode() is called.
  • getTablename()
    The name of the database table.
  • getAttributes()
    Return the list of object attributes.
  • getFields()
    Retrieve the fields of the object's database table along with their metadata.
  • setObjFromArray()
    Sets object properties from an array, excluding specified keys, and saves the object.

Example

Jump to BaseClass Example →