cakephp 3.0 using tableprefix?

余生颓废 提交于 2019-12-02 02:29:28

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());
    }
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!