- Author
- David Heath
- Date
- 23 Jul 03
Download a demo
here.
- Motivation
- An unneccessary amount of effort is involved in coding basic data storage and retrieval for new data object classes.
- Opportunity
- Much of this code is quite similar, so we could abstract and build at a higher level
- Proposal
- add a GenericDataObject class which would handle object storage/retrieval.
This proposal is to add a class called GenericDataObject in /classes/genericdataobject.php. I implemented an experimental version in the worldpay transaction class for the online donations module. This key data structure in this class is $parameterMetadata, which looks like this:
class eZWorldPayTransaction {
var $primaryKey = 'cartId';
var $parameterMetadata = array(
'cartId' => DBSTORAGE_INT4,
'instId' => DBSTORAGE_INT4,
'description' => DBSTORAGE_STRING,
'amount' => DBSTORAGE_DECIMAL,
'currency' => DBSTORAGE_STRING,
'accId' => DBSTORAGE_INT4,
'authMode' => DBSTORAGE_INT4,
...
);
function setAttribute($name, $value) {
if (array_key_exists($name, $this->parameterMetadata)) {
$this->$name = $value;
}
}
function getAttribute($name) {
if (array_key_exists($name, $this->parameterMetadata)) {
return $this->$name;
}
else {
return false;
}
}
}
This data structure is used to automate the insert/delete/update operations.
What does this achieve:
- only have to change this data structure to add another attribute (don't have to modify insert+update sql)
- allows data validation of parameters based on the metadata, which is not always done by the programmer
For the GenericDataObject, the derived class would provide $parmeterMetadata (either set it in the constructor, or have it taken from an overridden method). The derived class would not need to implement fill/get/store/delete. The derived class could override setAttribute/getAttribute if special behaviour was needed for particular attributes.