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\MysqlDialectTrait;
 18: use Cake\Database\Driver;
 19: use Cake\Database\Query;
 20: use Cake\Database\Statement\MysqlStatement;
 21: use PDO;
 22: 
 23: /**
 24:  * Class Mysql
 25:  */
 26: class Mysql extends Driver
 27: {
 28:     use MysqlDialectTrait;
 29: 
 30:     /**
 31:      * Base configuration settings for MySQL driver
 32:      *
 33:      * @var array
 34:      */
 35:     protected $_baseConfig = [
 36:         'persistent' => true,
 37:         'host' => 'localhost',
 38:         'username' => 'root',
 39:         'password' => '',
 40:         'database' => 'cake',
 41:         'port' => '3306',
 42:         'flags' => [],
 43:         'encoding' => 'utf8mb4',
 44:         'timezone' => null,
 45:         'init' => [],
 46:     ];
 47: 
 48:     /**
 49:      * The server version
 50:      *
 51:      * @var string
 52:      */
 53:     protected $_version;
 54: 
 55:     /**
 56:      * Whether or not the server supports native JSON
 57:      *
 58:      * @var bool
 59:      */
 60:     protected $_supportsNativeJson;
 61: 
 62:     /**
 63:      * Establishes a connection to the database server
 64:      *
 65:      * @return bool true on success
 66:      */
 67:     public function connect()
 68:     {
 69:         if ($this->_connection) {
 70:             return true;
 71:         }
 72:         $config = $this->_config;
 73: 
 74:         if ($config['timezone'] === 'UTC') {
 75:             $config['timezone'] = '+0:00';
 76:         }
 77: 
 78:         if (!empty($config['timezone'])) {
 79:             $config['init'][] = sprintf("SET time_zone = '%s'", $config['timezone']);
 80:         }
 81:         if (!empty($config['encoding'])) {
 82:             $config['init'][] = sprintf('SET NAMES %s', $config['encoding']);
 83:         }
 84: 
 85:         $config['flags'] += [
 86:             PDO::ATTR_PERSISTENT => $config['persistent'],
 87:             PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true,
 88:             PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
 89:         ];
 90: 
 91:         if (!empty($config['ssl_key']) && !empty($config['ssl_cert'])) {
 92:             $config['flags'][PDO::MYSQL_ATTR_SSL_KEY] = $config['ssl_key'];
 93:             $config['flags'][PDO::MYSQL_ATTR_SSL_CERT] = $config['ssl_cert'];
 94:         }
 95:         if (!empty($config['ssl_ca'])) {
 96:             $config['flags'][PDO::MYSQL_ATTR_SSL_CA] = $config['ssl_ca'];
 97:         }
 98: 
 99:         if (empty($config['unix_socket'])) {
100:             $dsn = "mysql:host={$config['host']};port={$config['port']};dbname={$config['database']};charset={$config['encoding']}";
101:         } else {
102:             $dsn = "mysql:unix_socket={$config['unix_socket']};dbname={$config['database']}";
103:         }
104: 
105:         $this->_connect($dsn, $config);
106: 
107:         if (!empty($config['init'])) {
108:             $connection = $this->getConnection();
109:             foreach ((array)$config['init'] as $command) {
110:                 $connection->exec($command);
111:             }
112:         }
113: 
114:         return true;
115:     }
116: 
117:     /**
118:      * Returns whether php is able to use this driver for connecting to database
119:      *
120:      * @return bool true if it is valid to use this driver
121:      */
122:     public function enabled()
123:     {
124:         return in_array('mysql', PDO::getAvailableDrivers(), true);
125:     }
126: 
127:     /**
128:      * Prepares a sql statement to be executed
129:      *
130:      * @param string|\Cake\Database\Query $query The query to prepare.
131:      * @return \Cake\Database\StatementInterface
132:      */
133:     public function prepare($query)
134:     {
135:         $this->connect();
136:         $isObject = $query instanceof Query;
137:         $statement = $this->_connection->prepare($isObject ? $query->sql() : $query);
138:         $result = new MysqlStatement($statement, $this);
139:         if ($isObject && $query->isBufferedResultsEnabled() === false) {
140:             $result->bufferResults(false);
141:         }
142: 
143:         return $result;
144:     }
145: 
146:     /**
147:      * {@inheritDoc}
148:      */
149:     public function schema()
150:     {
151:         return $this->_config['database'];
152:     }
153: 
154:     /**
155:      * {@inheritDoc}
156:      */
157:     public function supportsDynamicConstraints()
158:     {
159:         return true;
160:     }
161: 
162:     /**
163:      * Returns true if the server supports native JSON columns
164:      *
165:      * @return bool
166:      */
167:     public function supportsNativeJson()
168:     {
169:         if ($this->_supportsNativeJson !== null) {
170:             return $this->_supportsNativeJson;
171:         }
172: 
173:         if ($this->_version === null) {
174:             $this->_version = $this->_connection->getAttribute(PDO::ATTR_SERVER_VERSION);
175:         }
176: 
177:         return $this->_supportsNativeJson = version_compare($this->_version, '5.7.0', '>=');
178:     }
179: }
180: 
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