hswner
hswner

Reputation: 592

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

Upvotes: 3

Views: 2460

Answers (2)

Tomas Gonzalez
Tomas Gonzalez

Reputation: 1175

It took me a while to figure it out. I didn't find it very clear on the docs.

There is no need to change it every time on your default database.

Just add a database connection including your prefix in app/Config/database.php and select it when baking.

If available, use --connection, -c parameter indicating your database on the command (see bake's help)

Upvotes: 0

Mindaugas Norvilas
Mindaugas Norvilas

Reputation: 532

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

Upvotes: 1

Related Questions