Quick db_component example, plus misc magic

The db_component is a big part of zoop. For many applications, database calls are a large part the work being done. The db_component provides the interface to your data. You can either use the default connection and global functions, or make your own connections and use the methods from the database object. There are too many functions to document here(be sure to look at the documentation), but this is an example of how to set up multiple connections. It also shows a way to deal with miscellaneous functions that you may need throughout your application. Keep in mind that it's always a good idea to keep your sql calls inside objects that encapsulate your business objects/model. The next example may be dbobject, a helper for your database objects.

config.php:

define_once('db_RDBMS', 'pgsql');
define_once('db_Server', 'localhost');
define_once('db_Port', 5432);
define_once('db_Username', 'someuser');
define_once('db_Password', '');
define_once('db_Database', 'somedb');
 
define_once('other_RDBMS', 'pgsql');
define_once('other_Server', 'localhost');
define_once('other_Port', 5432);
define_once('other_Username', 'otheruser');
define_once('other_Password', '');
define_once('other_Database', 'otherdb');

includes.php:

$zoop->addComponent('db');
$zoop->addInclude('misc', app_dir . '/misc.php');

By creating misc as a class, and calling functions statically, you can get the added bonus of autoincludes in php5 for your global-style functions. Of course, you can rename misc the name of your application, and it's even cleaner.
misc.php:
class misc
{
	function &getOtherDb()
	{
		if(isset($GLOBALS['otherDB']))
			return $GLOBALS['otherDB'];
		$dsn = Database::makeDSN(other_RDBMS, other_host, other_port, other_username, other_password, other_database);
		$GLOBALS['otherDB'] = &new database($dsn);
		return $GLOBALS['otherDB'];
	}
}

zone_default.php:

zone_default extends zone
{
	function pageDefault()
	{
		sql_query();
		sql_insert();
		sql_check();
		sql_fetch_*();
		sql_begin_transaction();
		sql_commit_transaction();
		sql_rollback_transaction();
		sql_escape_string();
		$db = &misc::getOtherDb();
		$db->*();
	}
}

Hello

Thanks for sharing!

dominicgaines 29 Jun 2008