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

A new feature in Sitellite 4.2 is the ability to dynamically generate a series of objects and their relationships based on the saf.Database.Generic package and its capabilities.

What this means is that you can perform dynamic object-relational mapping, including proper management of the relationships between objects/tables, with the code generated automatically for you.

Introduction to Generic

The Generic package allows you to define simple objects which provide many built-in database accessor methods that your objects inherit from Generic. Generic abstracts away the need for SQL queries to interact with a database. For example, taking the following database schema:

CREATE TABLE myapp_products (
	id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
	name CHAR(72) NOT NULL,
	price DECIMAL(7,2) NOT NULL,
	category INT NOT NULL,
	description TEXT NOT NULL,
	index (category, price)
);

CREATE TABLE myapp_categories (
	id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
	name CHAR(72) NOT NULL,
	index (name)
);

You could create a few simple Generic-based objects with the following code:

<?php

loader_import ('saf.Database.Generic');

class Product extends Generic {
	function Product () {
		parent::Generic (
			'myapp_products', // table name
			'id' // primary key field
		);
	}
}

class Category extendsd Generic {
	function Category () {
		parent::Generic (
			'myapp_categories', // table name
			'id' // primary key field
		);
	}
}

?>

Typically you would save each class to its own file, however for simplicity we'll keep them together. If you save this to the file inc/app/myapp/lib/Product.php you could then call it from a box like this:

<?php

loader_import ('myapp.Product');

$category = new Category;

$cat_id = $category->add (
	array (
		'name' => 'Hosting Packages',
	)
);

$product = new Product;

$product->add (
	array (
		'name' => 'Basic Hosting',
		'price' => 9.95,
		'category' => $cat_id,
		'description' => '250MB Space, 5GB Transfer, etc.',
	)
);

?>

That's all the code necessary to create a new category and add a product to it. Not bad. Let's see how we might make this even easier using the new relational features of Generic 2.0.



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.