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

  • 相关阅读:
    [CodeForces]Codeforces Round #429 (Div. 2) ABC(待补)
    About Me
    2018-06-14
    Codeforces Codeforces Round #484 (Div. 2) E. Billiard
    Codeforces Codeforces Round #484 (Div. 2) D. Shark
    Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings
    Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes
    Codeforces Avito Code Challenge 2018 D. Bookshelves
    Codeforces Round #485 (Div. 2) D. Fair
    Codeforces Round #485 (Div. 2) F. AND Graph
  • 原文地址:https://www.cnblogs.com/lein317/p/5067639.html
Copyright © 2011-2022 走看看