Last active
December 2, 2015 22:57
-
-
Save burzum/06a0037c7ad8bcaf3333 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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