prado框架中application.xml使用
1.简单使用
关于application.xml:
<paths> <alias id="adodb" path="E:/Apache2/htdocs/adodb"/> <using namespace="adodb.*"/> <using namespace="Application.Controls.*"/> <using namespace="Application.DataObjects.*"/> <using namespace="System.Security.*"/> </paths> <parameter id="DSN" value="mysql://user:pwd@localhost/database"/> |
调用parameter DSN
$this->Database = NewADOConnection($this->Application->parameters['DSN']); $this->Database = NewADOConnection(Prado::getApplication->parameters['DSN']); |
现在项目中的Prado框架为改造过的框架,调用方式为
$this->Application->getUserParameter('DSN') |
2.以模块的方式使用
从网上查到的一个例子(版本为3.1.7):
这是一个在PRADO 3里面使Adodb可用(以模块方式)的方法。事实上它的用法始终与Prado 2版本很相似。
在页面中的使用:
<?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) 拷贝粘贴下列代码到一个叫adbdb.php的新文件中去并把所说的这个文件放到“protected/modules/Copy”目录中去。你可能必须重定向两个“require_once”路径,以便它们正确指出你的adodb文件夹。
dodb.php
<?php define('ADODB_ASSOC_CASE', 0); //为了方便,指示ADOdb总是返回小写的字段名。 require_once('../../../adodb/adodb-exceptions.inc.php'); require_once('../../../adodb/adodb.inc.php'); 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的魔术函数。 //这个方法将传递所有的方法调用给ADODB类/库。 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){ //详见: http://phplens.com/lens/adodb/docs-adodb.htm#pconnect $this->db->PConnect($this->Host, $this->Username, $this->Password, $this->Database); } else{ //详见: http://phplens.com/lens/adodb/docs-adodb.htm#connect $this->db->Connect($this->Host, $this->Username, $this->Password, $this->Database); } } return $this->db; } //参数的获取和设定方法 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) 现在要在我们需要的地方拥有该模块,请在application.xml中使它的命名空间可用并使当前模块可用(带链接参数)。
<?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> |