CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Team
    • Issues (Github)
    • YouTube Channel
    • Get Involved
    • Bakery
    • Featured Resources
    • Newsletter
    • Certification
    • My CakePHP
    • CakeFest
    • Facebook
    • Twitter
    • Help & Support
    • Forum
    • Stack Overflow
    • IRC
    • Slack
    • Paid Support
CakePHP

C CakePHP 3.8 Red Velvet API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 3.8
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

Namespaces

  • Cake
    • Auth
      • Storage
    • Cache
      • Engine
    • Collection
      • Iterator
    • Command
    • Console
      • Exception
    • Controller
      • Component
      • Exception
    • Core
      • Configure
        • Engine
      • Exception
      • Retry
    • Database
      • Driver
      • Exception
      • Expression
      • Schema
      • Statement
      • Type
    • Datasource
      • Exception
    • Error
      • Middleware
    • Event
      • Decorator
    • Filesystem
    • Form
    • Http
      • Client
        • Adapter
        • Auth
      • Cookie
      • Exception
      • Middleware
      • Session
    • I18n
      • Formatter
      • Middleware
      • Parser
    • Log
      • Engine
    • Mailer
      • Exception
      • Transport
    • Network
      • Exception
    • ORM
      • Association
      • Behavior
        • Translate
      • Exception
      • Locator
      • Rule
    • Routing
      • Exception
      • Filter
      • Middleware
      • Route
    • Shell
      • Helper
      • Task
    • TestSuite
      • Fixture
      • Stub
    • Utility
      • Exception
    • Validation
    • View
      • Exception
      • Form
      • Helper
      • Widget
  • None

Classes

  • Mysql
  • Postgres
  • Sqlite
  • Sqlserver

Traits

  • PDODriverTrait
  1: <?php
  2: /**
  3:  * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4:  * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5:  *
  6:  * Licensed under The MIT License
  7:  * For full copyright and license information, please see the LICENSE.txt
  8:  * Redistributions of files must retain the above copyright notice.
  9:  *
 10:  * @copyright     Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
 11:  * @link          https://cakephp.org CakePHP(tm) Project
 12:  * @since         3.0.0
 13:  * @license       https://opensource.org/licenses/mit-license.php MIT License
 14:  */
 15: namespace Cake\Database\Driver;
 16: 
 17: use Cake\Database\Dialect\SqlserverDialectTrait;
 18: use Cake\Database\Driver;
 19: use Cake\Database\Query;
 20: use Cake\Database\Statement\SqlserverStatement;
 21: use PDO;
 22: 
 23: /**
 24:  * SQLServer driver.
 25:  */
 26: class Sqlserver extends Driver
 27: {
 28:     use SqlserverDialectTrait;
 29: 
 30:     /**
 31:      * Base configuration settings for Sqlserver driver
 32:      *
 33:      * @var array
 34:      */
 35:     protected $_baseConfig = [
 36:         'host' => 'localhost\SQLEXPRESS',
 37:         'username' => '',
 38:         'password' => '',
 39:         'database' => 'cake',
 40:         'port' => '',
 41:         // PDO::SQLSRV_ENCODING_UTF8
 42:         'encoding' => 65001,
 43:         'flags' => [],
 44:         'init' => [],
 45:         'settings' => [],
 46:         'attributes' => [],
 47:         'app' => null,
 48:         'connectionPooling' => null,
 49:         'failoverPartner' => null,
 50:         'loginTimeout' => null,
 51:         'multiSubnetFailover' => null,
 52:     ];
 53: 
 54:     /**
 55:      * Establishes a connection to the database server.
 56:      *
 57:      * Please note that the PDO::ATTR_PERSISTENT attribute is not supported by
 58:      * the SQL Server PHP PDO drivers.  As a result you cannot use the
 59:      * persistent config option when connecting to a SQL Server  (for more
 60:      * information see: https://github.com/Microsoft/msphpsql/issues/65).
 61:      *
 62:      * @throws \InvalidArgumentException if an unsupported setting is in the driver config
 63:      * @return bool true on success
 64:      */
 65:     public function connect()
 66:     {
 67:         if ($this->_connection) {
 68:             return true;
 69:         }
 70:         $config = $this->_config;
 71: 
 72:         if (isset($config['persistent']) && $config['persistent']) {
 73:             throw new \InvalidArgumentException('Config setting "persistent" cannot be set to true, as the Sqlserver PDO driver does not support PDO::ATTR_PERSISTENT');
 74:         }
 75: 
 76:         $config['flags'] += [
 77:             PDO::ATTR_EMULATE_PREPARES => false,
 78:             PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
 79:         ];
 80: 
 81:         if (!empty($config['encoding'])) {
 82:             $config['flags'][PDO::SQLSRV_ATTR_ENCODING] = $config['encoding'];
 83:         }
 84:         $port = '';
 85:         if (strlen($config['port'])) {
 86:             $port = ',' . $config['port'];
 87:         }
 88: 
 89:         $dsn = "sqlsrv:Server={$config['host']}{$port};Database={$config['database']};MultipleActiveResultSets=false";
 90:         if ($config['app'] !== null) {
 91:             $dsn .= ";APP={$config['app']}";
 92:         }
 93:         if ($config['connectionPooling'] !== null) {
 94:             $dsn .= ";ConnectionPooling={$config['connectionPooling']}";
 95:         }
 96:         if ($config['failoverPartner'] !== null) {
 97:             $dsn .= ";Failover_Partner={$config['failoverPartner']}";
 98:         }
 99:         if ($config['loginTimeout'] !== null) {
100:             $dsn .= ";LoginTimeout={$config['loginTimeout']}";
101:         }
102:         if ($config['multiSubnetFailover'] !== null) {
103:             $dsn .= ";MultiSubnetFailover={$config['multiSubnetFailover']}";
104:         }
105:         $this->_connect($dsn, $config);
106: 
107:         $connection = $this->getConnection();
108:         if (!empty($config['init'])) {
109:             foreach ((array)$config['init'] as $command) {
110:                 $connection->exec($command);
111:             }
112:         }
113:         if (!empty($config['settings']) && is_array($config['settings'])) {
114:             foreach ($config['settings'] as $key => $value) {
115:                 $connection->exec("SET {$key} {$value}");
116:             }
117:         }
118:         if (!empty($config['attributes']) && is_array($config['attributes'])) {
119:             foreach ($config['attributes'] as $key => $value) {
120:                 $connection->setAttribute($key, $value);
121:             }
122:         }
123: 
124:         return true;
125:     }
126: 
127:     /**
128:      * Returns whether PHP is able to use this driver for connecting to database
129:      *
130:      * @return bool true if it is valid to use this driver
131:      */
132:     public function enabled()
133:     {
134:         return in_array('sqlsrv', PDO::getAvailableDrivers(), true);
135:     }
136: 
137:     /**
138:      * Prepares a sql statement to be executed
139:      *
140:      * @param string|\Cake\Database\Query $query The query to prepare.
141:      * @return \Cake\Database\StatementInterface
142:      */
143:     public function prepare($query)
144:     {
145:         $this->connect();
146:         $options = [PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL];
147:         $isObject = $query instanceof Query;
148:         if ($isObject && $query->isBufferedResultsEnabled() === false) {
149:             $options = [];
150:         }
151:         $statement = $this->_connection->prepare($isObject ? $query->sql() : $query, $options);
152: 
153:         return new SqlserverStatement($statement, $this);
154:     }
155: 
156:     /**
157:      * {@inheritDoc}
158:      */
159:     public function supportsDynamicConstraints()
160:     {
161:         return true;
162:     }
163: }
164: 
Follow @CakePHP
#IRC
OpenHub
Rackspace
  • Business Solutions
  • Showcase
  • Documentation
  • Book
  • API
  • Videos
  • Logos & Trademarks
  • Community
  • Team
  • Issues (Github)
  • YouTube Channel
  • Get Involved
  • Bakery
  • Featured Resources
  • Newsletter
  • Certification
  • My CakePHP
  • CakeFest
  • Facebook
  • Twitter
  • Help & Support
  • Forum
  • Stack Overflow
  • IRC
  • Slack
  • Paid Support

Generated using CakePHP API Docs