src/Shell/DbMaintenanceShell.php
public function tablePrefix($action = null, $prefix = null) {
		$db = $this->_getConnection();
		$config = $db->config();
		$database = $config['database'];
		if (!empty($this->params['database'])) {
			$database = $this->params['database'];
		}

		while (!$action || !in_array($action, ['A', 'R'], true)) {
			$action = $this->in('Add or remove?', ['A', 'R']);
		}

		while (!$prefix) {
			$prefix = $this->in('Please select prefix:');
		}

		$space = "\n";
		if ($action === 'R') {
			$length = mb_strlen($prefix) + 1;
			$script = <<<SQL
SELECT  CONCAT('RENAME TABLE `$database`.`', table_name, '` TO `$database`.`', SUBSTR(table_name, $length), '`;$space') AS statement
FROM    information_schema.tables AS tb
WHERE   table_schema = '$database'
AND     table_name LIKE '$prefix%'
AND     `TABLE_TYPE` = 'BASE TABLE';
SQL;
		} else {
			$script = <<<SQL
SELECT  CONCAT('RENAME TABLE `$database`.`', table_name, '` TO `$database`.`$prefix', table_name, '`;$space') AS statement
FROM    information_schema.tables AS tb
WHERE   table_schema = '$database'
AND     table_name NOT LIKE '$prefix%'
AND     `TABLE_TYPE` = 'BASE TABLE';
SQL;
		}

		/** @var \Cake\Database\Statement\StatementDecorator $res */
		$res = $db->query($script);
		if (!$res->count()) {
			$this->abort('Nothing to do...');
		}

		$script = '';
		foreach ($res as $r) {
			$script .= $r['statement'];
			$this->out($r['statement'], 1, Shell::VERBOSE);
		}

		if (!$this->param('dry-run')) {
			$continue = $this->in($res->count() . ' tables will be altered.', ['Y', 'N'], 'N');
			if (strtoupper($continue) !== 'Y') {
				$this->abort('Aborted!');
			}
		}

		if (!$this->params['dry-run']) {
			$db->query($script);
		} else {
			$this->out($script);
		}
		$this->out('Done :)');
	}
public function tablePrefix($action = null, $prefix = null) {
		$db = $this->_getConnection();
		$config = $db->config();
		$database = $config['database'];
		if (!empty($this->params['database'])) {
			$database = $this->params['database'];
		}

		while (!$action || !in_array($action, ['A', 'R'], true)) {
			$action = $this->in('Add or remove?', ['A', 'R']);
		}

		while (!$prefix) {
			$prefix = $this->in('Please select prefix:');
		}

		$space = "\n";
		if ($action === 'R') {
			$length = mb_strlen($prefix) + 1;
			$script = <<<SQL
SELECT  CONCAT('RENAME TABLE `$database`.`', table_name, '` TO `$database`.`', SUBSTR(table_name, $length), '`;$space') AS statement
FROM    information_schema.tables AS tb
WHERE   table_schema = '$database'
AND     table_name LIKE '$prefix%'
AND     `TABLE_TYPE` = 'BASE TABLE';
SQL;
		} else {
			$script = <<<SQL
SELECT  CONCAT('RENAME TABLE `$database`.`', table_name, '` TO `$database`.`$prefix', table_name, '`;$space') AS statement
FROM    information_schema.tables AS tb
WHERE   table_schema = '$database'
AND     table_name NOT LIKE '$prefix%'
AND     `TABLE_TYPE` = 'BASE TABLE';
SQL;
		}

		/** @var \Cake\Database\Statement\StatementDecorator $res */
		$res = $db->query($script);
		if (!$res->count()) {
			$this->abort('Nothing to do...');
		}

		$script = '';
		foreach ($res as $r) {
			$script .= $r['statement'];
			$this->out($r['statement'], 1, Shell::VERBOSE);
		}

		if (!$this->param('dry-run')) {
			$continue = $this->in($res->count() . ' tables will be altered.', ['Y', 'N'], 'N');
			if (strtoupper($continue) !== 'Y') {
				$this->abort('Aborted!');
			}
		}

		if (!$this->params['dry-run']) {
			$db->query($script);
		} else {
			$this->out($script);
		}
		$this->out('Done :)');
	}
public function tablePrefix ( $ action = null , $ prefix = null ) { $ db = $ this -> _getConnection ( ) ; $ config = $ db -> config ( ) ; $ database = $ config [ 'database' ] ; if ( ! empty ( $ this -> params [ 'database' ] ) ) { $ database = $ this -> params [ 'database' ] ; } while ( ! $ action || ! in_array ( $ action , [ 'A' , 'R' ] , true ) ) { $ action = $ this -> in ( 'Add or remove?' , [ 'A' , 'R' ] ) ; } while ( ! $ prefix ) { $ prefix = $ this -> in ( 'Please select prefix:' ) ; } $ space = "\n" ; if ( $ action === 'R' ) { $ length = mb_strlen ( $ prefix ) + 1 ; $ script =  <<<SQL
SELECT  CONCAT('RENAME TABLE `$database`.`', table_name, '` TO `$database`.`', SUBSTR(table_name, $length), '`;$space') AS statement
FROM    information_schema.tables AS tb
WHERE   table_schema = '$database'
AND     table_name LIKE '$prefix%'
AND     `TABLE_TYPE` = 'BASE TABLE';
SQL ; } else { $ script =  <<<SQL
SELECT  CONCAT('RENAME TABLE `$database`.`', table_name, '` TO `$database`.`$prefix', table_name, '`;$space') AS statement
FROM    information_schema.tables AS tb
WHERE   table_schema = '$database'
AND     table_name NOT LIKE '$prefix%'
AND     `TABLE_TYPE` = 'BASE TABLE';
SQL ; } /** @var \Cake\Database\Statement\StatementDecorator $res */ $ res = $ db -> query ( $ script ) ; if ( ! $ res -> count ( ) ) { $ this -> abort ( 'Nothing to do...' ) ; } $ script = '' ; foreach ( $ res as $ r ) { $ script .= $ r [ 'statement' ] ; $ this -> out ( $ r [ 'statement' ] , 1 , Shell :: VERBOSE ) ; } if ( ! $ this -> param ( 'dry-run' ) ) { $ continue = $ this -> in ( $ res -> count ( ) . ' tables will be altered.' , [ 'Y' , 'N' ] , 'N' ) ; if ( strtoupper ( $ continue ) !== 'Y' ) { $ this -> abort ( 'Aborted!' ) ; } } if ( ! $ this -> params [ 'dry-run' ] ) { $ db -> query ( $ script ) ; } else { $ this -> out ( $ script ) ; } $ this -> out ( 'Done :)' ) ; }
Adds or removes table prefixes.

Since CakePHP 3.0 does not support them, this is very useful when
migrating 2.x apps that use those prefixes.

@param string|null $action
@param string|null $prefix
@return void
Adds or removes table prefixes .