关于application.xml

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
|-------------------------------------------------------------------------------------------------------------
| 说明: 使adodb库能作为Prado的一个模块
|-------------------------------------------------------------------------------------------------------------
| 属性      类型
|-------------------------------------------------------------------------------------------------------------
| -$db         Obj. 一个adodb对象的实例(NewADOConnection)。
| -$_Driver        String. 连接类型。例如:mysql、mssql、oci8、odbc。
|          http://phplens.com/lens/adodb/docs-adodb.htm#DatabasesSupported
| -$_Host        String. 主机/服务器名称/IP等等。
| -$_Username       String. 认证用的数据库用户名。
| -$_Password       String. 认证用的数据库密码。
| -$_Database       String. 要连接的数据库名称。
| -$_Persistent = false     Boolean. 是否是持久连接?
|-------------------------------------------------------------------------------------------------------------
| 方法        DESCR/返回
|-------------------------------------------------------------------------------------------------------------
| +init($config)      Prado method. 从application.xml中以的值设定。
| +__call($method, $params)    PHP的魔术方法。传递产生到this对象的调用到$db。
| +getDatabaseConnection()    实例化一DB链接或返回已打开的DB链接。
| +getDriver()       返回$_Driver.
| +setDriver($value)     设定$_Driver.
| +getHost()       返回$_Host.
| +setHost($value)      设定$_Host.
| +getUsername()      返回$_Username.
| +setUsername($value)     设定$_Username.
| +getPassword()      返回$_Password.
| +setPassword($value)     设定$_Password.
| +getDatabase()      返回$_Database.
| +setDatabase($value)     设定$_Database.
| +getPersistent()      返回$_Persistent.
| +setPersistent($value)    设定$_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的魔术函数。
 //这个方法将传递所有的方法调用给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>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值