Zend Framework中如何实现MySQL的读写分离?

找到以下文件:/Zend/Db/Adapter/Pdo/Abstract.php

以下代码中标红的是追加的代码。

/**
* Prepares an SQL statement.
*
* @param string $sql The SQL statement with placeholders.
* @param array $bind An array of data to bind to the placeholders.
* @return PDOStatement
*/
public function prepare($sql)
{
// 读写分离开始
$first=strtolower(substr($sql,0,6));
if($first==”select”)
{
$this->_config=Zend_Registry::get(“db_read”);
}
else
{
$this->_config=Zend_Registry::get(“db_write”);
}

$db_type=$this->_config['db_type'];
$this->_connection=Zend_Db_Pool::get($db_type);

//读写分离结束

$this->_connect();
$stmtClass = $this->_defaultStmtClass;
if (!class_exists($stmtClass)) {
require_once ‘Zend/Loader.php’;
Zend_Loader::loadClass($stmtClass);
}

$stmt = new $stmtClass($this, $sql);
$stmt->setFetchMode($this->_fetchMode);
return $stmt;
}

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注

*

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>