rainlab/builder-plugin

How can I import external databases into the builder plugin?

Closed this issue · 13 comments

Since the Builder Plugin automatically makes the table name like 'author_pluginname_' and they automatically change as lowercases, I can't use any external database tables, especially their names are uppercases. I know I could if I change all the tables name, but for some reason, their name should remain as uppercase. OR is there any way I can use external databases for displaying them?

@2jiwon Unfortunately, the Builder plugin will only list tables that match the standard table prefix for your plugin, which is the author_pluginname_ prefix you state above. If you want external database tables to show in this editor, you will need to match that prefix in the table name.

Note that you can still use your custom tables in October, just that the Builder plugin will not display them or allow you to interact with them.

@LukeTowers Could you guide me how can I use the custom tables in October? I need to import the existing database tables but I can't find any way to do that.

@bennothommo Sorry to bother you, but could I edit the prefix rule for accept the table names are uppercases?

Hi @2jiwon - to answer your question to @LukeTowers, you can specify an external table by setting the $table property in your model. For instance, to use TEST_TABLE, it would be:

class Post extends Model
{
    protected $table = 'TEST_TABLE';
}

If this exists in a completely different database to the one you use for the rest of your October app, you will need to specify another database connection in your config/database.php file and add a property called $connection to your model with the name of your database connection in the config. You can see details about this here: https://laravel.com/docs/5.5/eloquent#defining-models

As for accepting uppercase table prefixes, I'm not sure if the prefix check is case-sensitive or not. If it is case-sensitive, I'd be happy to look over a PR to change that if you would like to submit one.

@bennothommo Thank you for your answer!! :D First of all, I tried to make table names as uppercases, they've been failed. I tried to find where I have to edit, but I'm a novice developer so I'm not sure if I can make the proper PR for that. T_T

Also, could you explain more about the way of using another database connection in config/database.php file? I checked the doc but I don't know what to do exactly.

@2jiwon to add another connection, just copy one of the existing definitions in https://github.com/octobercms/october/blob/master/config/database.php#L55-L65, edit the appropriate values, and then change the key to be whatever custom name you want for that connection. I.e.

'my_old_db' => [
    'driver'   => 'sqlite',
    'database' => 'storage/old-database.sqlite',
    'prefix'   => '',
],

Thank you @LukeTowers and @bennothommo! I figured out! You are lifesavers! Thank you very much!! :D

If you want external database tables to show in this editor, you will need to match that prefix in the table name.

Hello @bennothommo, to make appear a table from an external database (database referenced by a subsequent key in config/database.php/connections) what should be done beside prefixing the table with author_pluginname_?

@trepmag I don't think we're going to support trying to use the builder plugin with external database connections. The builder plugin is a development tool, so you should never be using it in a production environment (which is where you would theoretically be needing to reference external database connections).

@LukeTowers thanks for answering... Now I'm not sure to what here 'external database' refers to but I wasn't meaning of a "production only database" but only a database that exists beside the default one (still available in production) and which can be accessed with by its setting key (https://octobercms.com/docs/database/basics#accessing-connections).

@trepmag We only support the "default" connection when using the Builder plugin. There's no facility to allow creating models through the Builder that are stored in a different connection. You can of course manually configure the model to use another connection, but I believe it will no longer be modifiable through the Builder plugin if you do that.

@bennothommo ok, then I've manually created the models to be mapped to an alternate database connection and they correctly appear in the builder UI which allows to follow the usage of Forms fields, Lists fields, Controller, etc, ... Great ;-)!

Update: adding that table 'deferred_bindings' must exists on the alternate database...