Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug fix for MySQL 8.0 #1346

Open
wants to merge 1 commit into
base: 1.1
Choose a base branch
from
Open

Conversation

alexbowers
Copy link
Contributor

In MySQL 8.0 tinyint without a length defined returns no length, causing a notice to be thrown

In MySQL 8.0 tinyint without a length defined returns no length, causing a notice to be thrown
@alexbowers
Copy link
Contributor Author

alexbowers commented Mar 8, 2021

If you need this on your own projects, you can do it yourself.

Create a class in your own project, i've called mine MySqlOverride.

<?php

namespace app\overrides;

use lithium\data\source\database\adapter\MySql;

class MySqlOverride extends MySql
{
    protected function _column($real) {
		if (is_array($real)) {
			return $real['type'] . (isset($real['length']) ? "({$real['length']})" : '');
		}

		if (!preg_match('/(?P<type>\w+)(?:\((?P<length>[\d,]+)\))?/', $real, $column)) {
			return $real;
		}
		$column = array_intersect_key($column, ['type' => null, 'length' => null]);

		if (isset($column['length']) && $column['length']) {
			$length = explode(',', $column['length']) + [null, null];
			$column['length'] = $length[0] ? (integer) $length[0] : null;
			$length[1] ? $column['precision'] = (integer) $length[1] : null;
		}

		switch (true) {
			case in_array($column['type'], ['date', 'time', 'datetime', 'timestamp']):
				return $column;
			case ($column['type'] === 'tinyint' && isset($column['length']) && $column['length'] == '1'):
			case ($column['type'] === 'boolean'):
				return ['type' => 'boolean'];
			break;
			case (strpos($column['type'], 'int') !== false):
				$column['type'] = 'integer';
			break;
			case (strpos($column['type'], 'char') !== false || $column['type'] === 'tinytext'):
				$column['type'] = 'string';
			break;
			case (strpos($column['type'], 'text') !== false):
				$column['type'] = 'text';
			break;
			case (strpos($column['type'], 'blob') !== false || $column['type'] === 'binary'):
				$column['type'] = 'binary';
			break;
			case preg_match('/float|double|decimal/', $column['type']):
				$column['type'] = 'float';
			break;
			default:
				$column['type'] = 'text';
			break;
		}
		return $column;
	}
}

And in connections.php, change your connection adapter to reference the class.

use app\overrides\MySqlOverride;

Connections::add(
    'default', [
        'persistent' => false,
        'type'     => 'database',
        'adapter'  => MySqlOverride::class,
        'host'     => getenv('database.host'),
        'database' => getenv('database.name'),
        'login'    => getenv('database.user'),
        'password' => getenv('database.pass'),
        'encoding' => 'UTF-8'
    ]
);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant