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\Query;
 18: use Cake\Database\Statement\PDOStatement;
 19: use PDO;
 20: use PDOException;
 21: 
 22: /**
 23:  * PDO driver trait
 24:  *
 25:  * @deprecated 3.6.0 The methods of this trait have been added to `Driver` class.
 26:  */
 27: trait PDODriverTrait
 28: {
 29:     /**
 30:      * Instance of PDO.
 31:      *
 32:      * @var \PDO|null
 33:      */
 34:     protected $_connection;
 35: 
 36:     /**
 37:      * Establishes a connection to the database server
 38:      *
 39:      * @param string $dsn A Driver-specific PDO-DSN
 40:      * @param array $config configuration to be used for creating connection
 41:      * @return bool true on success
 42:      */
 43:     protected function _connect($dsn, array $config)
 44:     {
 45:         $connection = new PDO(
 46:             $dsn,
 47:             $config['username'],
 48:             $config['password'],
 49:             $config['flags']
 50:         );
 51:         $this->connection($connection);
 52: 
 53:         return true;
 54:     }
 55: 
 56:     /**
 57:      * Returns correct connection resource or object that is internally used
 58:      * If first argument is passed, it will set internal connection object or
 59:      * result to the value passed
 60:      *
 61:      * @param \PDO|null $connection The PDO connection instance.
 62:      * @return \PDO connection object used internally
 63:      */
 64:     public function connection($connection = null)
 65:     {
 66:         if ($connection !== null) {
 67:             $this->_connection = $connection;
 68:         }
 69: 
 70:         return $this->_connection;
 71:     }
 72: 
 73:     /**
 74:      * Disconnects from database server
 75:      *
 76:      * @return void
 77:      */
 78:     public function disconnect()
 79:     {
 80:         $this->_connection = null;
 81:     }
 82: 
 83:     /**
 84:      * Checks whether or not the driver is connected.
 85:      *
 86:      * @return bool
 87:      */
 88:     public function isConnected()
 89:     {
 90:         if ($this->_connection === null) {
 91:             $connected = false;
 92:         } else {
 93:             try {
 94:                 $connected = $this->_connection->query('SELECT 1');
 95:             } catch (PDOException $e) {
 96:                 $connected = false;
 97:             }
 98:         }
 99: 
100:         return (bool)$connected;
101:     }
102: 
103:     /**
104:      * Prepares a sql statement to be executed
105:      *
106:      * @param string|\Cake\Database\Query $query The query to turn into a prepared statement.
107:      * @return \Cake\Database\StatementInterface
108:      */
109:     public function prepare($query)
110:     {
111:         $this->connect();
112:         $isObject = $query instanceof Query;
113:         $statement = $this->_connection->prepare($isObject ? $query->sql() : $query);
114: 
115:         return new PDOStatement($statement, $this);
116:     }
117: 
118:     /**
119:      * Starts a transaction
120:      *
121:      * @return bool true on success, false otherwise
122:      */
123:     public function beginTransaction()
124:     {
125:         $this->connect();
126:         if ($this->_connection->inTransaction()) {
127:             return true;
128:         }
129: 
130:         return $this->_connection->beginTransaction();
131:     }
132: 
133:     /**
134:      * Commits a transaction
135:      *
136:      * @return bool true on success, false otherwise
137:      */
138:     public function commitTransaction()
139:     {
140:         $this->connect();
141:         if (!$this->_connection->inTransaction()) {
142:             return false;
143:         }
144: 
145:         return $this->_connection->commit();
146:     }
147: 
148:     /**
149:      * Rollback a transaction
150:      *
151:      * @return bool true on success, false otherwise
152:      */
153:     public function rollbackTransaction()
154:     {
155:         $this->connect();
156:         if (!$this->_connection->inTransaction()) {
157:             return false;
158:         }
159: 
160:         return $this->_connection->rollBack();
161:     }
162: 
163:     /**
164:      * Returns a value in a safe representation to be used in a query string
165:      *
166:      * @param mixed $value The value to quote.
167:      * @param string $type Type to be used for determining kind of quoting to perform
168:      * @return string
169:      */
170:     public function quote($value, $type)
171:     {
172:         $this->connect();
173: 
174:         return $this->_connection->quote($value, $type);
175:     }
176: 
177:     /**
178:      * Returns last id generated for a table or sequence in database
179:      *
180:      * @param string|null $table table name or sequence to get last insert value from
181:      * @param string|null $column the name of the column representing the primary key
182:      * @return string|int
183:      */
184:     public function lastInsertId($table = null, $column = null)
185:     {
186:         $this->connect();
187: 
188:         return $this->_connection->lastInsertId($table);
189:     }
190: 
191:     /**
192:      * Checks if the driver supports quoting, as PDO_ODBC does not support it.
193:      *
194:      * @return bool
195:      */
196:     public function supportsQuoting()
197:     {
198:         $this->connect();
199: 
200:         return $this->_connection->getAttribute(PDO::ATTR_DRIVER_NAME) !== 'odbc';
201:     }
202: }
203: 
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