Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use table prefix for CakePHP plugins?

I want to write a plugin, which has its own database tables. Without creating the tables from a schema.php file, I create them manually. Then I want to bake the models for that plugin. But I also want cake bake shell to recognize prefixed table names and understand that they belong to that plugin, so that only prefixed tables are shown in the options of the shell.

To clarify, plugins may have their own schema. So, sometimes table names of different plugins may conflict. In order to avoid any conflicts, I guess table prefixes can be used. Is it possible to use table prefixes for CakePHP plugins? If the answer is yes, then how can I configure table prefix for a plugin? My preference is not to touch app/Config/database.php file. Instead, it would be better to set any configuration in the plugin itself.

P.S. I am using CakePHP 2.4.1

like image 536
hswner Avatar asked Mar 24 '26 00:03

hswner


1 Answers

Use $tablePrefix in your plugin model:

<?php
// Plugin/Example/Model/ExampleAppModel.php

App::uses('AppModel', 'Model');
class ExampleAppModel extends AppModel {

/**
 *  Table prefix
 */
    public $tablePrefix = 'example_';
}

Then, all your models in Example plugin will use example_ prefix

like image 115
Mindaugas Norvilas Avatar answered Mar 28 '26 01:03

Mindaugas Norvilas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!