Skip to content

Instantly share code, notes, and snippets.

@burzum
Last active December 2, 2015 22:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save burzum/06a0037c7ad8bcaf3333 to your computer and use it in GitHub Desktop.
Save burzum/06a0037c7ad8bcaf3333 to your computer and use it in GitHub Desktop.
<?php
namespace App\Model\Table;
use Cake\Core\Configure;
/**
* Database Prefix Trait for CakePHP 3.0
*
* @author Florian Krämer
* @license MIT
*/
trait TablePrefixTrait {
/**
* The table prefix to use.
*
* @var string
*/
protected $_tablePrefix = '';
/**
* Getter and setter for the DB prefix
*
* @param string $prefix
* @return string
*/
public function dbPrefix($prefix = '') {
if (empty($prefix)) {
$connectionConfig = $this->connection()->config();
if (isset($connectionConfig['prefix'])) {
$prefix = $connectionConfig['prefix'];
}
}
if (!empty($prefix)) {
$this->_tablePrefix = $prefix;
}
return $this->_tablePrefix;
}
/**
* Returns the database table name or sets a new one
*
* @param string|null $table the new table name
* @return string
*/
public function table($table = null) {
if (!empty($table)) {
$table = $this->_tablePrefix . $table;
}
return parent::table($table);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment