Documentation

Table Handler

The TableHandler class is designed to facilitate the creation and management of database tables using a PHP object-oriented approach.
It provides methods to define table structures, add columns and foreign keys, create and drop tables, and generate PHP class files that represent the database tables in a format compatible with the Boostack framework.

Usage Example

$tableHandler = new \Boostack\Models\Database\TableHandler();
$tableHandler->dropTable("offer");
$tableHandler->setTableName("offer");
$tableHandler->addColumn('id', 'INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY');
$tableHandler->addColumn('id_customer', 'INT(11) NOT NULL');
$tableHandler->addColumn('id_project', 'INT(11) NULL DEFAULT NULL');
$tableHandler->addColumn('id_product', 'INT(11) NOT NULL');
$tableHandler->addColumn("name", "varchar(255) NOT NULL");
$tableHandler->addColumn("subject", "TEXT NOT NULL");
$tableHandler->addColumn("date", "DATE NOT NULL");
$tableHandler->addColumn("amount", "FLOAT NOT NULL");
$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->addIndex("id_customer");
$tableHandler->addIndex("id_project");
#$tableHandler->addForeignKey("id_customer", "gds_customer", "id"); // set up the foreign key sql relations like this
#$tableHandler->addForeignKey("id_project", "gds_project", "id"); //set up the foreign key sql relations like this
#$tableHandler->addForeignKey("id_product", "gds_product", "id"); //set up the foreign key sql relations like this

// Create the table in the database
$tableHandler->createTable();

// Generate the PHP class file for the table
$className = "ExampleTable";
$tableHandler->generateClassFileFromDatabase($className);

// Generate the PHP list class file for the table
$tableHandler->generateListClassFile($className);

Methods

  • __construct($objUser = NULL)
    Initializes the TableHandler instance with a PDO instance.
    • $objUser: Optional. A user object, default is NULL.
    • This method returns void: No return value.
  • setTableName($tableName)
    Sets the name of the table.
    • $tableName: The name of the table.
    • This method returns void: No return value.
  • addColumn($columnName, $definition)
    Adds a column definition to the table.
    • $columnName: The name of the column.
    • $definition: The definition of the column (e.g., data type, constraints).
    • This method returns void: No return value.
  • addIndex($columnName, $indexType)
    Adds an index to a column.
    • $columnName: The name of the column.
    • $indexType: The type of the index (e.g., 'INDEX', 'UNIQUE', 'PRIMARY KEY').
    • This method returns void: No return value.
  • addForeignKey($columnName, $referencedTable, $referencedColumn, $onDelete = 'CASCADE', $onUpdate = 'CASCADE')
    Adds a foreign key definition to the table.
    • $columnName: The name of the column that will be a foreign key.
    • $referencedTable: The table that the foreign key references.
    • $referencedColumn: The column in the referenced table.
    • $onDelete: Action on delete (default is 'CASCADE').
    • $onUpdate: Action on update (default is 'CASCADE').
    • This method returns void: No return value.
  • createTable()
    Creates the table in the database based on the defined columns and foreign keys.
    • This method returns void: No return value.
  • reset()
    Resets the table definition by clearing the table name, columns, and foreign keys.
    • This method returns void: No return value.
  • dropTable($tableName)
    Drops a specified table from the database.
    • $tableName: The name of the table to drop.
    • This method returns void: No return value.
  • dropAllTables()
    Drops all tables in the database.
    • This method returns void: No return value.
  • generateClassFileFromDatabase($className, $namespace = "My\Models")
    Generates and saves the PHP class file for the table based on database information.
    • $className: The name of the class (e.g., "[ClassName]").
    • $namespace: The namespace for the class (default is "My\Models").
    • This method returns void: No return value.
  • generateListClassFile($className, $namespace = "My\Models")
    Generates and saves the PHP list class file for the table based on database information.
    • $className: The name of the main class (e.g., "[ClassName]").
    • $namespace: The namespace for the list class (default is "My\Models").
    • This method returns void: No return value.