在/Zend/Db/Adapter/Pdo/Abstract中的一个query改写成:
![](/Images/OutliningIndicators/ContractedBlock.gif)
Code
public function query($sql, $table_name=array(), $bind = array())
{
if (is_array($bind)) {
foreach ($bind as $name => $value) {
if (!is_int($name) && !preg_match('/^:/', $name)) {
$newName = ":$name";
unset($bind[$name]);
$bind[$newName] = $value;
}
}
}
try {
/**
* This is a DataBase visit Other DataBase Content
* auther WangBin
*/
if(!empty($table_name)){
$config = Zend_Registry::get('config');
foreach ( $table_name as $value_name){
$temp_name .= "select dblink_connect('".$value_name."','dbname=$value_name host=".$config->db->params->host." user=".$config->db->params->username." password=".$config->db->params->password."'); ";
}
return parent::query($temp_name.$sql, $table_name, $bind);
}else{
return parent::query($sql, $table_name, $bind);
}
} catch (PDOException $e) {
/**
* @see Zend_Db_Statement_Exception
*/
require_once 'Zend/Db/Statement/Exception.php';
throw new Zend_Db_Statement_Exception($e->getMessage());
}
}
Zend/Db/Adapter/Abstract.php
![](/Images/OutliningIndicators/ContractedBlock.gif)
Code
//把函数:
public function query($sql, $bind = array())
{}
//改成:
public function query($sql, $table_name=array(), $bind = array())
{..}
public function insert($table, array $bind)
{
//把: $stmt = $this->query($sql, array_values($bind)); 改成:
$stmt = $this->query($sql, array(), array_values($bind));
}
public function update($table, array $bind, $where = '')
{
//把: $stmt = $this->query($sql, array_values($bind)); 改成:
$stmt = $this->query($sql, array(), array_values($bind));
}
public function fetchAll($sql, $bind = array(), $fetchMode = null)
{
//把: $stmt = $this->query($sql, array_values($bind)); 改成:
$stmt = $this->query($sql, array(), array_values($bind));
}
public function fetchRow($sql, $bind = array(), $fetchMode = null)
{
//把: $stmt = $this->query($sql, array_values($bind)); 改成:
$stmt = $this->query($sql, array(), array_values($bind));
}
public function fetchAssoc($sql, $bind = array())
{
//把:$stmt = $this->query($sql, array(), $bind);改成:
$stmt = $this->query($sql, array(), array_values($bind));
}
public function fetchCol($sql, $bind = array())
{
//把:$stmt = $this->query($sql, array(), $bind);改成:
$stmt = $this->query($sql, array(), array_values($bind));
}
public function fetchPairs($sql, $bind = array())
{
//把:$stmt = $this->query($sql, array(), $bind);改成:
$stmt = $this->query($sql, array(), array_values($bind));
}
public function fetchOne($sql, $bind = array())
{
//把:$stmt = $this->query($sql, array(), $bind);改成:
$stmt = $this->query($sql, array(), array_values($bind));
}
config.ini配置:
; Database Settings
db.adapter = pdo_pgsql ; Zend_Db 驱动名
db.params.host = 192.168.1.1 ; 数据库主机地址
db.params.username = postgres ; 数据库用户名
db.params.password = 123456 ; 数据库密码
db.params.dbname = test ; 数据库空间名
可以多表访问:
$db = zend_registry::get('db');
//schema是数据的表空间,test是这个数据库中的表.
$result = $db->query("select * from test;",array('schema')); //array()中可以传入多个schema
$db->query()直接调用的是Zend/Db/Adapter/Pdo/Abstract.php中的query()函数,
我在query函数中加了一个是否和其它DB连接的判断,如果传进来一个DB名,就自动进行POSTGRESQL里的自带的dblink,自动去找那个数据库.
为了方便,我把其它DB中的数据库表都做为这个数据库的VIEW,每个数据表都建立了一个视图.
这样就可以很方便的查找到其它数据中的信息,而不需要修改了.