<?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); } } ?>
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); } } ?>