ADOdb module tutorial


Ini adalah sebuah cara untuk membuat koneksi ADOdb dalam Prado 3. Sebenarnya penggunaan nya sangat mirip ADODB dalam Prado 2. K.I.S.S.

Library yang diperlukan : Untuk source ADOdb dan dokumentasinya bisa kita download dari: http://adodb.sourceforge.net/

Pengunaan tag-nya sebagai berikut :

 
<?php
class home extends TPage{
	public function onLoad($param){
		$db = $this->Application->getModule('adodb');
		$sql = 'SELECT email FROM user WHERE user_id=1';
		echo $db->GetOne($sql);
	}
}
?>

1) Copy dan paste kode dibawah ini ke dalam file baru yang dinamai adodb.php dan simpan ke dalam direktori “protected\modules\”. Kita juga harus mengarahkan dua path  “require_once”, sehingga keduanya dengan tepat menunjuk ke folder adodb tersebut.

adodb.php

 
<?php
define('ADODB_ASSOC_CASE', 0); //Instructs ADOdb to always return lowercase field names. For Portability.

require_once('../../../adodb/adodb-exceptions.inc.php');
require_once('../../../adodb/adodb.inc.php');

/*
|-------------------------------------------------------------------------------------------------------------
| CLASS: adodb
|-------------------------------------------------------------------------------------------------------------
| DESCRIPTION: Makes adodb library available as a Prado module.
|-------------------------------------------------------------------------------------------------------------
| PROPERTY								TYPE
|-------------------------------------------------------------------------------------------------------------
| -$db									Obj. An instance of the adodb object (NewADOConnection).
| -$_Driver								String. Connection type. For example: mysql; mssql, oci8, odbc.
|										http://phplens.com/lens/adodb/docs-adodb.htm#DatabasesSupported
| -$_Host								String. Host/server name/IP/etc.
| -$_Username							String. Database username for authentication.
| -$_Password							String. Database password for authentication.
| -$_Database							String. Name of database to connect to.
| -$_Persistent = false					Boolean. Persistent connection?
|-------------------------------------------------------------------------------------------------------------
| METHOD								DESCR/RETURN
|-------------------------------------------------------------------------------------------------------------
| +init($config)						Prado method.
| +__call($method, $params)				PHP Magic method. Passes calls made to this object into $db.
| +getDatabaseConnection()				Instatiates a DB connection or returns the already open DB connection.
| +getDriver()							Returns $_Driver.
| +setDriver($value)					Sets $_Driver.
| +getHost()							Returns $_Host.
| +setHost($value)						Sets $_Host.
| +getUsername()						Returns $_Username.
| +setUsername($value)					Sets $_Username.
| +getPassword()						Returns $_Password.
| +setPassword($value)					Sets $_Password.
| +getDatabase()						Returns $_Database.
| +setDatabase($value)					Sets $_Database.
| +getPersistent()						Returns $_Persistent.
| +setPersistent($value)				Sets $_Persistent.
|-------------------------------------------------------------------------------------------------------------
*/
class adodb extends TModule{
	private $db;
	private $_Driver;
	private $_Host;
	private $_Username;
	private $_Password;
	private $_Database;
	private $_Persistent = false;

	public function init($config){
		if (!$this->Driver){
			throw new TConfigurationException('Missing param: Driver');
		}
		if (!$this->Host){
			throw new TConfigurationException('Missing param: Host');
		}
		if (!$this->Username){
			throw new TConfigurationException('Missing param: Username');
		}
		if (!$this->Password){
			throw new TConfigurationException('Missing param: Password');        
		}
		if (!$this->Database){
			throw new TConfigurationException('Missing param: Database');        
		}
		parent::init($config);
	}

	//PHP magic function.
	//This method will pass all method calls to ADODB class/library.
	public function __call($method, $params){
		$conn = $this->getDatabaseConnection();
		return call_user_func_array(array($conn, $method), $params);
	}

	private function getDatabaseConnection(){
		if (!isset($this->db)){		
			$this->db = NewADOConnection($this->Driver);
			$this->db->SetFetchMode(ADODB_FETCH_ASSOC);
			if ($this->Persistent){
				//For more see: http://phplens.com/lens/adodb/docs-adodb.htm#pconnect
				$this->db->PConnect($this->Host, $this->Username, $this->Password, $this->Database);
			}
			else{
				//For more see: http://phplens.com/lens/adodb/docs-adodb.htm#connect
				$this->db->Connect($this->Host, $this->Username, $this->Password, $this->Database);
			}
		}
		return $this->db;
	}

	//Getter and setters for params.
	public function getDriver(){
		return $this->_Driver;
	}
	public function setDriver($value){
		$this->_Driver = TPropertyValue::ensureString($value);
	}
	public function getHost(){
		return $this->_Host;
	}
	public function setHost($value){
		$this->_Host = TPropertyValue::ensureString($value);
	}
	public function getUsername(){
		return $this->_Username;
	}
	public function setUsername($value){
		$this->_Username = TPropertyValue::ensureString($value);
	}
	public function getPassword(){
		return $this->_Password;
	}
	public function setPassword($value){
		$this->_Password = TPropertyValue::ensureString($value);
	}
	public function getDatabase(){
		return $this->_Database;
	}
	public function setDatabase($value){
		$this->_Database = TPropertyValue::ensureString($value);
	}
	public function getPersistent(){
		return $this->_Persistent;
	}
	public function setPersistent($value){
		$this->_Persistent = TPropertyValue::ensureBoolean($value);
	}
}
?>

2)Sekarang  kita telah mempunyai modul yang diberinama application.xml dan membuat modul yang sebenarnya sudah tersedia ( dengan parameter koneksi).


<?xml version="1.0" encoding="iso-8859-1"?>
<application id="myAppId" Mode="Debug">
	<paths>
		<using namespace="Application.modules.*" />
		...
	</paths>

	<modules>
		<module id="adodb" class="adodb"
			Driver="mysql" Host="localhost" Username="usernamehere" Password="passwordhere" Database="databasenamehere" />
	</modules>

        ...
</application>

VIOLA!

Originally Ditulis Oleh by Fragmaster B.

Please feel free to contact me with any comments/questions.

~ oleh desktopku pada Oktober 15, 2008.

Satu Tanggapan to “ADOdb module tutorial”

  1. Hi Your web page runs up literally slow in my opinion, I not really know who’s
    issue is that however facebook starts relatively fast.

    Anyway, Thanks for placing an incredibly excellent article.

    Everyone who actually visited this site must have found this informative article
    extremely handy. I personally have to mention that you have done fantastic work with this and expect to check out many more wonderful stuff through you.
    To get more information through articles which you write-up, I have saved to bookmarks this web page.

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s

 
%d blogger menyukai ini: