Moving on from a cakephp 2 application, I am stuck on trying to use $tablePrefix in my models. Is this still available in Cake 3? Or how can I use something else (please explain).
Table prefixes are gone for now. There is an open PR, however it's not clear at this point when, and if at all this is going to make it:
https://github.com/cakephp/cakephp/pull/4505
As mentioned by Lorenzo in the comments, an alternative might be using events to initialize models with a prefixed table name:
For people still wanting support for prefixes in their tables, there is a way to support it using events. This will probably work for all table operations. Add it to your bootstrap.php file:
EventManager::instance()->on('Model.initialize', function ($event, $instance) { $instance->table('prefix_' . $instance->table()); });
https://github.com/cakephp/cakephp/pull/4505#issuecomment-76154855
To get prefix per table like you did in 2.x
just do this in your Table class:
public function initialize(array $config) {
$this->table('my_prefix_' . $this->table());
...
}
I'm migrating some cake2.x code to cake3. A slight alteration to ndm's code has worked for me so far in bootstrap.php, along these lines:
Cake\Event\EventManager::instance()->on('Model.initialize', function ($event) {
$instance = $event->subject;
if ($instance->defaultConnectionName() == 'default') {
$instance->table('prefix_' . $instance->table());
}
});
来源:https://stackoverflow.com/questions/28792720/cakephp-3-0-using-tableprefix