[kohana] kohana3.2,如何兼容PDO数据库连接方式

由于历史原因,有个kohana3.2的站点需要搬迁到php5.5上来,但php5.5已经不支持mysql_connect()这个函数了,只能使用PDO来连接数据库。

但换上PDO之后,报了这个的一个错:

Kohana_Exception [ 0 ]: Database method list_columns is not supported by Kohana_Database_PDO

居然这么恶心的事情都出现 -_-!

经过一轮google,终于找到解决方案了。

1. 在你的MODPATH或者APPPATH,创建新文件,classes/Database/PDO/MySQL.php,代码如下

<?php

/*
* PHP version 5.5
*
* @copyright Copyright (c) 2012-2015 友客OA Inc. (http://www.yokeoa.com)
* @link http://www.yokeoa.com
* @license 友客OA版权所有
*/ /**
* 我是类描述信息哦!
*
* @author birdylee <birdylee_cn@163.com>
* @since 2015年00月26日
* @version 1.0
*
*/
defined('SYSPATH') or die('No direct script access.'); class Database_PDO_MySQL extends Database_PDO
{
public function list_columns($table, $like = NULL, $add_prefix = TRUE)
{
// Quote the table name
$table = ($add_prefix === TRUE) ? $this->quote_table($table) : $table; if (is_string($like)) {
// Search for column names
$result = $this->query(Database::SELECT, 'SHOW FULL COLUMNS FROM ' . $table . ' LIKE ' . $this->quote($like), FALSE);
} else {
// Find all column names
$result = $this->query(Database::SELECT, 'SHOW FULL COLUMNS FROM ' . $table, FALSE);
} $count = 0;
$columns = array();
foreach ($result as $row) {
list($type, $length) = $this->_parse_type($row['Type']); $column = $this->datatype($type); $column['column_name'] = $row['Field'];
$column['column_default'] = $row['Default'];
$column['data_type'] = $type;
$column['is_nullable'] = ($row['Null'] == 'YES');
$column['ordinal_position'] = ++$count; switch ($type) { //was $column['type']
case 'float':
if (isset($length)) {
list($column['numeric_precision'], $column['numeric_scale']) = explode(',', $length);
}
break;
case 'int':
if (isset($length)) {
// MySQL attribute
$column['display'] = $length;
}
break;
case 'string':
switch ($column['data_type']) {
case 'binary':
case 'varbinary':
$column['character_maximum_length'] = $length;
break;
case 'char':
case 'varchar':
$column['character_maximum_length'] = $length;
case 'text':
case 'tinytext':
case 'mediumtext':
case 'longtext':
$column['collation_name'] = $row['Collation'];
break;
case 'enum':
case 'set':
$column['collation_name'] = $row['Collation'];
$column['options'] = explode('\',\'', substr($length, 1, -1));
break;
}
break;
} // MySQL attributes
$column['comment'] = $row['Comment'];
$column['extra'] = $row['Extra'];
$column['key'] = $row['Key'];
$column['privileges'] = $row['Privileges']; $columns[$row['Field']] = $column;
} return $columns;
} }

2. 你需要设置使用新的驱动类,%path%/database/config/database.php,将'type'由PDO换成PDO_MySQL

    'default' => array
(
'type' => 'PDO_MySQL',
'connection' => array(
'dsn' => 'mysql:dbname=test;host=127.0.0.1',
'username' => 'root',
'password' => '123456',
'persistent' => FALSE,
),
'table_prefix' => '',
'charset' => 'utf8',
'caching' => FALSE,
),
上一篇:Linux 网卡驱动设备程序设计(1)


下一篇:WebSocket 浅析