PHP code that creates tableObjects, which are objects that correspond to a table row.
Let's say you have a table in a MySQL database named category. To write into that row you would (traditionally) connect to the datbase with PDO then create a PDO::Statement, then exec the statement with the data coming from an array. If successful, you have a new category in the table.
In my case that meant having lots of arrays floating around, and arrays in PHP can be damn near anything. Which means lots of bugs and errors when interacting with the DB.
The tableObject is a generated class. It is generated by looking at the table for the tableObject.
If the category
table looks like this:
Field | Type | Null | Key | Default | Extra |
---|---|---|---|---|---|
category_id | int(11) | NO | PRI | NULL | auto_increment |
category_name | varchar(100) | NO | UNI | NULL | |
created_at | datetime | NO | NULL | ||
modified_at | timestamp | NO | CURRENT_TIMESTAMP |
Then the CategoryTableObject will have getters and setters for each field (column name)
For example
$catTO = new CategoryTableObject();
$catTO->setCategoryName('foo');
You can load an array into a tableObject. The array keys have to match the database fields:
$data = array(
'category_name'=> 'foo',
);
$catTo = new CategoryTableObject($data));
echo $catTo->getCategoryName(); // foo
By design, the tableObjects do not include any logic. They are designed only to be structured holders of data. They are easily resused and they meet the requirements I have of making the interactions with the database more deterministic.