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:
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 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
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;
}
fill($array)
clearAndFill($array)
load($id)
exist($id)
save()
save($id)
)delete()
softDelete()
otherwise call purge()
to permanently delete from database. restore()
lockTable()
unlockTable()
methodunlockTable()
jsonSerialize()
getTablename()
getAttributes()
getFields()
setObjFromArray()
Jump to BaseClass Example →