Prado学习备忘录(3)
时间:2006-07-13 来源:saillee
原著:Fragmaster B
翻译:Sail Lee
原文见:
Prado Wiki
译者按:本文给出了一个在应用中以模块方式调用Adodb数据库抽象类的实例,在PRADO的开发版本(Lastest版本可从官方Subversion源码库下载)中,已经加入了一现成的TAdodb模块,可提供相同的作用,用户可以直接使用TAdodb达到相同的目的。因此,本文仅对下载官方发布的3.0.2或以下3.0版本的用户有参考作用。特此说明。
这是一个在PRADO 3里面使Adodb可用(以模块方式)的方法。事实上它的用法始终与Prado 2版本很相似。
在页面中的使用:
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文件夹。
adodb.php
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中使它的命名空间可用并使当前模块可用(带链接参数)。
...
...
翻译:Sail Lee
原文见:
Prado Wiki
译者按:本文给出了一个在应用中以模块方式调用Adodb数据库抽象类的实例,在PRADO的开发版本(Lastest版本可从官方Subversion源码库下载)中,已经加入了一现成的TAdodb模块,可提供相同的作用,用户可以直接使用TAdodb达到相同的目的。因此,本文仅对下载官方发布的3.0.2或以下3.0版本的用户有参考作用。特此说明。
这是一个在PRADO 3里面使Adodb可用(以模块方式)的方法。事实上它的用法始终与Prado 2版本很相似。
在页面中的使用:
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文件夹。
adodb.php
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中使它的命名空间可用并使当前模块可用(带链接参数)。
...
...
相关阅读 更多 +