简体   繁体   中英

Laravel 4 - is there a way to have a prefix for some tables, but not others?

Most of my database tables use a prefix set in the database config file, but I have several tables that are static that I use for lookups like States, Countries, etc. Is there a way to tell Laravel in the models or elesewhere that these tables do NOT use the prefix Right now my seeders and other features don't work because they assume the prefix.

I have the table names set in the models like this:

protected $table = 'states';

Of course I could either just make them use the prefix like all the other tables or create a separate database connection, but I'm wondering if there's another solution.

Thanks!

I was thinking about doing this for a project as well and I came upon this thread:

http://laravel.io/forum/03-20-2014-override-table-prefix-for-one-model

The last comment (as of right now at least) suggests setting up another db connection that uses an alternate (or no) prefix, then tell the specific models you want to use that connection.

eg in the model:

protected $connection = 'db_no_prefix';    
protected $table = 'this_table';   

in db config

'connections' = > [
    ...
    'db_no_prefix' => [
        ....
        'prefix' => '', 
    ],
],

There's no way Laravel (or any software in existence) would magically know which of your tables have prefixes and which ones don't if you don't define it somewhere. The place to define it is exactly where you said, in the model:

// State model
protected $table = 'states';

// Some other model
protected $table = 'pre_mytable';

If your models all clearly have their tables defined, your seeds should work perfectly.

Now, obviously if some piece of software had a list of database tables defined somewhere, it could iterate through them and determine which ones were prefixed. But this ultimately defeats the purpose since the original intention was for your software to figure out which ones were prefixed so it could know the table name to access.

Anyways, its a good practice to explicitly define your table names in the models. Someday down the road when you or someone else looks at your code, you might wonder what table a model is referring to, but if it's clearly defined then there is no way to be confused. Never try to rely on too much magic on an app if you want to understand things a year later.

I think part of the problem is in my seeders. I'm not using the Schema class to create the table, but am using it when dropping.

class CreateStatesTable extends Migration {

    public function up()
    {
        DB::statement("
            CREATE TABLE IF NOT EXISTS `".Config::get('database.connections.mysql.prefix')."_states` (
              `state_id` tinyint(2) NOT NULL AUTO_INCREMENT,
              `state_name` varchar(15) NOT NULL DEFAULT '',
              `state_abbr` char(2) NOT NULL DEFAULT '',
              PRIMARY KEY (`state_id`)
            ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=52 ;
        ");
    }

    public function down()
    {
        Schema::drop('states');
    }
}

However, even if I solve this, I may still have problems in other parts of the system. Probably easier just to use the prefix and create duplicates of the tables in other apps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM