Documentation

Course: Model Example

Example

Create a table in the database and name it, for example, "boostack_course" with the following SQL structure:

CREATE TABLE `boostack_course` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `description` text,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  `last_update` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  `last_access` timestamp NOT NULL DEFAULT current_timestamp(),
  `deleted_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

Optionally, you can create the table in the database using the Boostack Table Handler

 $tableHandler = new \Boostack\Models\Database\TableHandler();
 $tableHandler->dropTable("boostack_course");
 $tableHandler->setTableName("boostack_course");
 $tableHandler->addColumn('id', 'INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY');
 $tableHandler->addColumn("name", "varchar(255) NOT NULL");
 $tableHandler->addColumn("description", "TEXT");
 $tableHandler->addColumn('created_at', 'TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP()');
 $tableHandler->addColumn('last_update', 'TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP() ON UPDATE current_timestamp()');
 $tableHandler->addColumn('last_access', 'TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP()');
 $tableHandler->addColumn('deleted_at', 'TIMESTAMP NULL DEFAULT NULL');
 $tableHandler->createTable();

Create a folder named "Courses" in the my/Models folder.
Create a new PHP file named Course.php in the my/Models/Courses folder.

For example, the Course.php file should look like this:

<?php
namespace My\Models\Courses;
class Course extends \Boostack\Models\BaseClassTraced {
    //do not set the $id field. It is already set by default
    protected $name;
    protected $description;
    protected $date_start;
    protected $date_end;
    //do not set the created_at, last_update, last_access and deleted_at fields. They are already set by default by BaseListTraced

    protected $default_values = [
        "name" => "",
        "description" => "",
        "date_start" => NULL,
        "date_end" => NULL
    ];

    const TABLENAME = "boostack_course";

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