About Sitellite       Screenshots       Downloads       Forge      Documentation       Community       Support

You are here: Home / Documentation / Dynamic Object-Generation with saf.Database.Generic

Dynamic Object-Generation with saf.Database.Generic

Custom Objects

The 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:

  • Highly readable -- Generic makes your code transparent and obvious in its purpose.
  • Highly object-oriented -- Generic makes MVC-structured applications the norm.
  • Highly flexible -- Generic provides loads of functionality not seen here (see the SAF documentation for a complete list of methods provided by Generic).
  • Highly reusable -- API's based on Generic all provide a standard set of methods, and being object-oriented and based on Sitellite, are easily reused anywhere in Sitellite or outside of it via SAF.


Page 1: Introduction to Generic
Page 2: Generic Table Relations
Page 3: A Complete Example
Page 4: Custom Objects

All Tutorials

Members

Note: You can use your SitelliteForge.com account here and vice versa.

Username

Password

Forgot your password?

Not a member? Click here to register

Sitellite 5 Beta


Copyright © 2008, SIMIAN systems Inc.
All rights reserved. Privacy policy
Some of the icons on this site were created by the Gnome Project.