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

  • 相关阅读:
    java.lang.OutOfMemoryError: GC overhead limit exceeded
    Ural 1146 Maximum Sum(DP)
    [算法系列之四]优先级队列
    Python源代码--整数对象(PyIntObject)的内存池
    3星|何帆《猜测与偏见》:社科书评集
    4星|《认识经济》:全面系统的经济学普及读物,鸿篇巨制,价格超贵
    3星|《东方启动点》:民营企业家们的故事,故事多分析概括少
    2星|《大师的管理课》:畅销书作家们的35篇励志散文
    3.5星|《蓝海战略2》:实施蓝海战略的具体工具与方法。案例牵强且偏老旧
    2星|《读懂华为30年》:基于公开资料的整理和肤浅的发挥
  • 原文地址:https://www.cnblogs.com/lein317/p/5067639.html
Copyright © 2011-2022 走看看