zend framework与php 5.3.8的pdo sql server 2008

zend1.1.1 在php 5.3.8环境下,目前支持sql 的pdo连接方式有问题,

需要修改:

1 Zend\Db\Adapter\Pdo\Mssql.php:

$_pdoType为->protected $_pdoType = 'sqlsrv';

2 Zend\Db\Adapter\Pdo\Abstract.php 的_connect函数

 

protected function _connect() { // if we already have a PDO object, no need to re-connect. if ($this->_connection) { return; } // get the dsn first, because some adapters alter the $_pdoType $dsn = $this->_dsn(); // check for PDO extension if (!extension_loaded('pdo')) { /** * @see Zend_Db_Adapter_Exception */ require_once 'Zend/Db/Adapter/Exception.php'; throw new Zend_Db_Adapter_Exception('The PDO extension is required for this adapter but the extension is not loaded'); } // check the PDO driver is available if (!in_array($this->_pdoType, PDO::getAvailableDrivers())) { /** * @see Zend_Db_Adapter_Exception */ require_once 'Zend/Db/Adapter/Exception.php'; throw new Zend_Db_Adapter_Exception('The ' . $this->_pdoType . ' driver is not currently installed'); } // create PDO connection $q = $this->_profiler->queryStart('connect', Zend_Db_Profiler::CONNECT); // add the persistence flag if we find it in our config array if (isset($this->_config['persistent']) && ($this->_config['persistent'] == true)) { $this->_config['driver_options'][PDO::ATTR_PERSISTENT] = true; } try { if(!isset($this->_config['ismssql'])||!$this->_config['ismssql']){ $this->_connection = new PDO( $dsn, $this->_config['username'], $this->_config['password'], $this->_config['driver_options'] ); }else{ $this->_connection = new PDO( "sqlsrv:server=".$this->_config['host'].";Database = ".$this->_config['dbname'], $this->_config['username'], $this->_config['password']); $this->_connection->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); $this->_connection->setAttribute( PDO::SQLSRV_ATTR_ENCODING, PDO::SQLSRV_ENCODING_UTF8 ); } $this->_profiler->queryEnd($q); // set the PDO connection to perform case-folding on array keys, or not $this->_connection->setAttribute(PDO::ATTR_CASE, $this->_caseFolding); // always use exceptions. $this->_connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch (PDOException $e) { /** * @see Zend_Db_Adapter_Exception */ require_once 'Zend/Db/Adapter/Exception.php'; throw new Zend_Db_Adapter_Exception('#------'.__LINE__.'->'.iconv('gb2312','utf-8',$e->getMessage()), $e->getCode(), $e); } }


 

修改上上面两个地方,连接和查询没有问题了

然后 limit 分页会有问题,所有要分页的查询都必须使用order by,否则分页无效,

最后当在最后一页,分页出来会满的,会把倒数第二页的填满最后一页

修改 Zend\Db\Adapter\Pdo\Mssql.php limit函数修改为下面的,并且要传入总的记录数,$totalitems是总的记录数:

public function limit($sql, $count, $offset = 0, $totalitems=null) { $count = intval($count); if ($count <= 0) { /** @see Zend_Db_Adapter_Exception */ require_once 'Zend/Db/Adapter/Exception.php'; throw new Zend_Db_Adapter_Exception("LIMIT argument count=$count is not valid"); } $offset = intval($offset); if ($offset < 0) { /** @see Zend_Db_Adapter_Exception */ require_once 'Zend/Db/Adapter/Exception.php'; throw new Zend_Db_Adapter_Exception("LIMIT argument offset=$offset is not valid"); } $realy_count=$count; if($totalitems!=null&&$count+$offset>$totalitems) { $sql = preg_replace( '/^SELECT\s+(DISTINCT\s)?/i', 'SELECT $1TOP ' . ($totalitems) . ' ', $sql ); $realy_count=$totalitems-$offset; }else{ $sql = preg_replace( '/^SELECT\s+(DISTINCT\s)?/i', 'SELECT $1TOP ' . ($count+$offset) . ' ', $sql ); } if ($offset > 0) { $orderby = stristr($sql, 'ORDER BY'); if ($orderby !== false) { $orderParts = explode(',', substr($orderby, 8)); $pregReplaceCount = null; $orderbyInverseParts = array(); foreach ($orderParts as $orderPart) { $orderPart = rtrim($orderPart); $inv = preg_replace('/\s+desc$/i', ' ASC', $orderPart, 1, $pregReplaceCount); if ($pregReplaceCount) { $orderbyInverseParts[] = $inv; continue; } $inv = preg_replace('/\s+asc$/i', ' DESC', $orderPart, 1, $pregReplaceCount); if ($pregReplaceCount) { $orderbyInverseParts[] = $inv; continue; } else { $orderbyInverseParts[] = $orderPart . ' DESC'; } } $orderbyInverse = 'ORDER BY ' . implode(', ', $orderbyInverseParts); } $sql = 'SELECT * FROM (SELECT TOP ' . $realy_count . ' * FROM (' . $sql . ') AS inner_tbl'; if ($orderby !== false) { $sql .= ' ' . $orderbyInverse . ' '; } $sql .= ') AS outer_tbl'; if ($orderby !== false) { $sql .= ' ' . $orderby; } } return $sql; }


 

转载于:https://www.cnblogs.com/Iamlein/archive/2011/10/06/2375901.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值