ADOdb module tutorial
From PRADO Wiki
Here's one way to make ADOdb available (as a Module) in Prado 3. Actual usage ends up being very similar to ADOdb usage in Prado 2. K.I.S.S.
Required External Libraries: ADOdb source downloads and documentation: http://adodb.sourceforge.net/
Usage in pages:
<?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 and paste the following code into a new file called adodb.php and place said file into "protected\modules\". You may have to redirect the two "require_once" paths so they correctly point to your adodb folder.
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)Now that we have the module in place we need to make its namespace available in application.xml and make the actual module available (with connection parameters).
<?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!
Next Steps: Use your new adodb module along with AdodbCache to get a performance boost!
Originally written by Fragmaster B.
Please feel free to contact me with any comments/questions.

