This is just a shot in the dark, but I am coding myself down a fiery vortex of doom. I have an ecommerce framework that is “working” and I am trying to improve it and make it easier to manage by converting it to a series of classes that get “serialized” to a MySQL db. SO we have class_order, class_customer, class_product, etc. One of the main things I need to implement is a “transform” method that will read through the existing (very flat and yucky) db schema and reformat everything into this nice new O-O relational schema. As such, I have embedded the table creation into each individual class, along with their corresponding relation table. So eg:
public function create_table() {
$q = "`id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
`type` SMALLINT NULL,
`value` decimal(6,2) DEFAULT NULL,
`usage_count` int null,
CONSTRAINT code_unique UNIQUE (discount_code)";
$ret = $this->actually_create_table($q);
return $ret;
}
This seems to me to be a great way to document the code within itself.
The problem is that in my “transformer” code, I have to instantiate each class in turn, 1st with empty data to generate the schema with the create_table() method and then just loop through all the records and let the new code do all of its magix. This initial “init with empty data, then call create_table()” setup is cloojy, but “fine”, except when I started to implement checks against the init data. So for example, with customer, if you pass in data with no email, let’s throw an exception bc we don’t want that data in the db. So now I have broken my transformer code, unless I give it “real” data and then just not “store” that. That would probably work, but it cranks the dial on CLoOj is just not very satisfying.
I then started to try to make these methods static so I could avoid instantiation. Well, that leads me to have to staticify lots of other methods and data and eventually I hit a wall with, for just one example, my parent DB class that has all the db related stuff (such as that actually_create_table() method) that all these classes need, including an instantiated $pdo member that obvz cannot be static.
I am now just stuck and wondering if anyone out there has solved a similar design problem bc searching the intarwebs has been fruitless.