|
You are here: Home / Documentation / Dynamic Object-Generation with saf.Database.Generic |
Dynamic Object-Generation with saf.Database.GenericCustom ObjectsThe only limitation with objects generated from an INI file is that there is no way to define custom methods within the object definition. To solve this problem, Generic defines two additional INI values you can use to cause your objects to inherit from intermediate objects which would then inherit from Generic. These are the import and extends values. The following is a simple example of a custom method being added to our Product object: lib/CustomProduct.php
<?php
loader_import ('saf.Database.Generic');
class CustomProduct extends Generic {
function addTax () {
// Manitoban tax is 7% PST and 7% GST:
return $this->val ('price') * 1.14;
}
}
?>
As you can see, this is pretty much a standard Generic-based object, nothing fancy. Now let's hook it up to our dynamic object: lib/Objects.ini.php ; <?php /* [Product] table = myapp_products pkey = id ; permissions = on ; uncomment this if your table has the sitellite_* fields import = myapp.CustomProduct extends = CustomProduct [Category] table = myapp_categories pkey = id [rel:Category:Product] type = 1x ; one-to-many Product field = category cascade = on ; delete products when a category is deleted ; */ ?> Your Product object should now inherit the custom addTax() method. As you can see, Generic makes it very easy to create applications quickly and with very little code. Plus, code based on Generic has the following additional benefits:
Page 1: Introduction to Generic |
|
Copyright © 2008, SIMIAN systems Inc. All rights reserved. Privacy policy Some of the icons on this site were created by the Gnome Project. |