Ajax and Ajax Frameworks

I have been a developer for nearly 20 years and a web developer for about 10 years. Over the years, I have considered creating a blog to share the things I have learned (and am learning) about web design and development, but I never seemed to have the time. When I began learning about web development using Ajax and Ajax Frameworks, I decided to take the time to create this blog.

My intention for this blog is to focus primarily on Ajax Frameworks, but since web development requires knowledge of many technologies, I will occasionally write about things such as CSS, Javascript, PHP, MySQL, Flash, etc.

Lately, I have been using JQuery as my primary Ajax tool. Although I have used other Ajax Frameworks in the past (Dojo Toolkit, Yahoo! User Interface Library, Scriptaculous/Prototype), JQuery has thus far been the easiest to learn. In my first few posts I will discuss some of the JQuery Plugins that I have created. Some of these include: a plugin for Google Maps (jquery.imGoogleMaps), Form validation and submission  (jquery.imValidateForm), Page Populater (jquery.imPagePopulate), and a plugin to create lists (jquery.imList).

While I am still learning about some of the other Ajax Frameworks, I hope that what I have learned will be helpful to others. Just remember, “When you can pull the pebbles from my hand…”

Share

CakePHP: Setting Up Multiple DataSources

Tuesday, October 27th, 2009

I am working on a project using CakePHP where I initially created JSON objects to store the data. Once I began to add the JSON data to a MySQL database, I grew weary of all the input. I decided to keep one the JSON objects as is. It was data that would never change (and a lot of data). Fortunately with CakePHP you can use multiple DataSources in a project. And the DataSources don’t have to be an actual database, you can use JSON (or XML for that mater).

Database Setup

First, open your database file (app/config/database.php). Add your DataSource after the default DataSource. I added a DataSource named jsonCategories.

class DATABASE_CONFIG {
	var $default = array(
		'driver' => 'mysql',
		'persistent' => false,
		'host' => '127.0.0.1',
		'port' => '',
		'login' => 'guestUser',
		'password' => 'Lf5nwENtAMpKDFVm',
		'database' => 'digitalvilliage',
		'schema' => '',
		'prefix' => '',
		'encoding' => ''
	);
 
	var $jsonCategories = array(
		'datasource' => 'categories',
		'readonly' => true,
		'file' => 'categories.js' 
	);
}

I named the datasource the same as the file name (categories), but I don’t think this is necessary. In this case, my datasource is a .js file.

DataSource File

Next I created the DataSource file (categories.js). This file is saved in webroot/files/categories.js.

?View Code JAVASCRIPT
[
{"name": "automotive", "label": "auto, ..."}
...
]

CakePHP DataSource File

In order for CakePHP to be able to interact with your DataSource, you have to create a DataSource file. The name of your source file must the datasource value that you entered when you set up the datasource in the database.php file. You also must add ‘_source’ to the name (categories_source.php). The file must be saved under app/models/datasources/.

class CategoriesSource extends DataSource {
    	var $description = 'Categories DataSource';
	var $File = null;
	var $FileUtil = null;
 
	function __construct($config=null) {
		parent::__construct($config);
		$this->connected = $this->connect();
		return $config;
	}
 
	function __destruct() {
		$this->connected = $this->close();
		parent::__destruct();
	}
 
	function connect() {
		App::import('Core','File');
		$this->FileUtil =& new File(WWW_ROOT.'files/'.$this->config['file']);
		$this->File = $this->FileUtil->read();
		if (!$this->File) {
			return false;
		} else {
			return true;
		}
	}
 
	function findAll() {
		return $this->File;
	}	
 
	function close() {
		if ($this->FileUtil->close()) {
			return false;
		} else {
			return true;
		}
	}
 
	function read() {}
 
	function query() {}
 
	function describe() {}
 
	function column() {}
 
	function isConnected() {}
 
	function showLog() {}
}

Name your class the same as the datasource value in your database.php (with Source as the suffix).

You need a constructor and destructor, but the most important methods are connect() and findAll(). As the name suggests, the connect() method enables Cake to connect to your datasource. In this case, you are just using the File utility to read the JSON file:

function connect() {
	App::import('Core','File');
	$this->FileUtil =& new File(WWW_ROOT.'files/'.$this->config['file']);
	$this->File = $this->FileUtil->read();
	if (!$this->File) {
		return false;
	} else {
		return true;
	}
}

The findAll() method will be used in the model (or controller).

Using the DataSource

So in my controller file, I have a method named getCategories. I use CakePHP’s Connection Manager to connect to my DataSource. I then call findAll() to retrieve the data.

function getCategories() {
	if ( $this->RequestHandler->isAjax() ) {
	   Configure::write ( 'debug', 0 );
	}
	$this->autoRender = false;
	App::import('ConnectionManager');
	$cats =& ConnectionManager::getDataSource("jsonCategories"); 
	return $cats->findAll();
}

That’s it. The autoRender and debug statements are used because I’m calling this method via Ajax. See my previous post regarding the usage.

So, as you can see, creating multiple datasources with CakePHP is rather straightforward. Enjoy.

Share

3 Responses to “CakePHP: Setting Up Multiple DataSources”

  • Spobin Says:

    Hi there!
    Is your text meant to be in grey? Your article was very informative but also very difficult to read. You should consider changing the text colour to white, or something with higher contrast.
    .-= Spobin´s last blog ..The worst example of web form validation ever =-.

  • admin Says:

    You are probably the 4th person to tell me that. I finally changed text to a lighter color (although I prefer the darker text). Thanks.

  • CakePHP: Baking on Ubuntu | GrasshopperPebbles.com Says:

    [...] I already setup the database configuration (app/config/database.php) and created my MySQL tables, I entered: ?View Code BASH//let cake know [...]

CommentLuv badge