Altering Database Tables
As a module grows, so does its database requirements. There have been many strategies to deal with the inevitable database changes, from custom .sql files to handwritten queries. Xmf\Database\Tables
is an object oriented approach to a standard solution to this common problem.
Rename a Database Table
This scenario references a module called 'pedigree' and we are going to define an update function that will rename an old table named 'eigenaar' to a standardized name of 'pedigree_owner'.
First, the old way:
Here is the same operation using the \Xmf\Database\Tables class:
Add a Column to a Table
Let's extend the previous example and suppose we also want to add a column, named 'registrar_code', as a varchar(24) at the same time. Here is the new function:
Going Further
There are methods available to handle any needed table changes. Tables can be created, dropped, and altered. You can modify multiple tables in one pass. All modifications go into a queue, so you can halt the entire process easily if you find something is amiss. Refer to the reference documentation for Xmf\Database\Tables for details.
There are companion objects, too. Xmf\Database\TableLoad can assist in adding data to tables. Xmf\Database\Migrate builds on Tables, adding automated schema comparison and synchronization.
It is worth considering these classes whenever you need to add or modify tables for your module.
Last updated