Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions lib/Cake/Model/Datasource/Database/Mysql.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,14 +163,23 @@ public function connect() {
$config = $this->config;
$this->connected = false;

// PDO::MYSQL_ATTR_* constants are deprecated in PHP 8.5; Pdo\Mysql class is available since PHP 8.1
if (class_exists('Pdo\Mysql')) {
$mysqlAttrUseBufferedQuery = Pdo\Mysql::ATTR_USE_BUFFERED_QUERY;
$mysqlAttrInitCommand = Pdo\Mysql::ATTR_INIT_COMMAND;
} else {
$mysqlAttrUseBufferedQuery = PDO::MYSQL_ATTR_USE_BUFFERED_QUERY;
$mysqlAttrInitCommand = PDO::MYSQL_ATTR_INIT_COMMAND;
}
Comment on lines +166 to +173

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(For the follow-up PR.) Pdo\Mysql was added in PHP 8.4.0, not 8.1 (https://www.php.net/manual/en/class.pdo-mysql.php).
PHP_VERSION_ID >= 80400 would also be preferable to class_exists('Pdo\Mysql'). On PHP < 8.4, class_exists() runs the autoloader chain on every connect(), and a userland Pdo\Mysql class would be misdetected. CakePHP 4.x/5.x use the same version check. A possible shape, which also defines the SSL variables used at L185-189:

		// PDO::MYSQL_ATTR_* constants are deprecated as of PHP 8.5 in favor of Pdo\Mysql::ATTR_* (available since PHP 8.4)
		if (PHP_VERSION_ID >= 80400) {
			$mysqlAttrUseBufferedQuery = Pdo\Mysql::ATTR_USE_BUFFERED_QUERY;
			$mysqlAttrInitCommand = Pdo\Mysql::ATTR_INIT_COMMAND;
			$mysqlAttrSslKey = Pdo\Mysql::ATTR_SSL_KEY;
			$mysqlAttrSslCert = Pdo\Mysql::ATTR_SSL_CERT;
			$mysqlAttrSslCa = Pdo\Mysql::ATTR_SSL_CA;
		} else {
			$mysqlAttrUseBufferedQuery = PDO::MYSQL_ATTR_USE_BUFFERED_QUERY;
			$mysqlAttrInitCommand = PDO::MYSQL_ATTR_INIT_COMMAND;
			$mysqlAttrSslKey = PDO::MYSQL_ATTR_SSL_KEY;
			$mysqlAttrSslCert = PDO::MYSQL_ATTR_SSL_CERT;
			$mysqlAttrSslCa = PDO::MYSQL_ATTR_SSL_CA;
		}


$flags = $config['flags'] + array(
PDO::ATTR_PERSISTENT => $config['persistent'],
PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true,
$mysqlAttrUseBufferedQuery => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
);

if (!empty($config['encoding'])) {
$flags[PDO::MYSQL_ATTR_INIT_COMMAND] = 'SET NAMES ' . $config['encoding'];
$flags[$mysqlAttrInitCommand] = 'SET NAMES ' . $config['encoding'];
}
if (!empty($config['ssl_key']) && !empty($config['ssl_cert'])) {
$flags[PDO::MYSQL_ATTR_SSL_KEY] = $config['ssl_key'];

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(For the follow-up PR.) PDO::MYSQL_ATTR_SSL_KEY / SSL_CERT (here) and SSL_CA (L189) are deprecated in PHP 8.5 as well (php-src UPGRADING for 8.5: "Driver specific constants in the PDO class have been deprecated"). SSL-enabled connections (e.g. ssl_ca for RDS/Cloud SQL) will therefore still emit E_DEPRECATED on every connect. With the variables above:

		if (!empty($config['ssl_key']) && !empty($config['ssl_cert'])) {
			$flags[$mysqlAttrSslKey] = $config['ssl_key'];
			$flags[$mysqlAttrSslCert] = $config['ssl_cert'];
		}
		if (!empty($config['ssl_ca'])) {
			$flags[$mysqlAttrSslCa] = $config['ssl_ca'];
		}

CakePHP 4.x/5.x switch these three as well.

Expand Down
Loading