zoukankan      html  css  js  c++  java
  • 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;
        }


    我的QQ群:

    PHPer&Webgame&移动开发,群号:95303036

  • 相关阅读:
    Selenium2Library系列 keywords 之 _SelectElementKeywords 之 _get_labels_for_options(self, options)
    Selenium2Library系列 keywords 之 _SelectElementKeywords 之_get_select_list_options(self, select_list_or_locator)
    Selenium2Library系列 keywords 之 _SelectElementKeywords 之_get_select_list(self, locator)
    Selenium2Library中的Get Alert Message
    selenium+testNG+Ant
    Maven安装testNG
    在FOR中使用close window,循环次数大于1就会报异常
    时间格式去‘0’
    序列化和反序列化组件 基表的使用 多表断关联关系分析 多表序列化组件 自定义子序列化深度连表 多表反序列化 序列化与反序列化整合 群增接口实现 单删群删接口实现 单整体改 单与群局部改
    drf安装与封装风格 drf请求生命周期 drf五大模块(请求模块,渲染模块,解析模块,异常处理模块,响应模块)
  • 原文地址:https://www.cnblogs.com/lein317/p/5067639.html
Copyright © 2011-2022 走看看