lonnieezell / Bonfire

Jumpstart your CodeIgniter web applications with a modular, HMVC-ready, backend.
http://cibonfire.com
1.39k stars 525 forks source link

Module Builder allow different db_prefixes #388

Open SirSteve opened 12 years ago

SirSteve commented 12 years ago

I would like to see something in place during the install for legacy CI sites. I am still confused on how to take my pre-existing CI site and use bonfire with it.

og-shawn-crigger commented 12 years ago

What exactly would you like in place for this? I do believe this is kinda beyond the scope of what Bonfire is made for honestly, if you could provide a list of features we could maybe implement some of them. Bonfire was never designed to be a Out of the Box anything, it is designed to be lightweight and offers just some bare-bones features that allow developing applications faster.

Converting from a Current Website to Bonfire is up to the user, we offer a excellent Module Builder that allows you to take you pre-existing Database tables and create modules with them, but these are just basic modules, you still have to customize the code inside of each module to fit its purpose.

I think maybe someone could write a guide on this, I might even make one after .6 is pushed into the develop branch.

SirSteve commented 12 years ago

This would go along the lines of NOT having to use the SAME db prefix as the legacy site.

I am also confused because BF has a users section already so can we hook into this one for our existing users or do we need a completely new module to do it?

og-shawn-crigger commented 12 years ago

This shouldn't be in the Issue tracker, have you read and fully understood

CodeIgniter's User Guide Bonfire's Docs Bonfire Guides HMVC Extension Docs

You can either use the current user system as for admin's only, or you could convert YOUR current user system to Bonfire's system, where you would have access to our permissions, manage, etc.

In the Model you can always switch the db prefix around with something like

<?php

    $old_prefix = $this->db->dbprefix;;
    $this->db->dbprefix = 'myfix';

    //Do whatever you have to do here

    // Reset the prefix the way it was
   $this->db->dbprefix = $old_prefix;
   // I always unset my variables, but you can code however you want.
   unset ( $old_prefix ); 

Bonfire is not made to automagicly convert your old applications into new ones, please continue this discussion on the forums were it belongs.

og-shawn-crigger commented 12 years ago

I didn't know this was in reference to the forums already forum link

Should probably rename the issue as Feature Request - Module Builder use Different DB Prefixs

afarazit commented 12 years ago

I could probably work on that. Have a db prefix option in config with default "bf_" and also when creating a module an input with the db prefix for new table (default value from config).

og-shawn-crigger commented 12 years ago

//cc @lonnieezell @seandowney What do you all think of the idea?

lonnieezell commented 12 years ago

Yeah, I like the option and think it's a needed one when converting existing db tables. I can see it working where you only have the option when you're specifying to build it from an existing table. Then it would default to whatever the db settings are set for.

Maybe even have the option of renaming the table to match the current prefix?

og-shawn-crigger commented 12 years ago

I like the option of renaming the DB prefix to match the current scheme, otherwise it sounds a bit confusing to have 2 different prefixes.

afarazit commented 12 years ago

Pull request #405 have nothing to do with DB prefix. It shouldn't be referenced here.

Pull request #405 gives you the option to use table name as field prefix or not. For example if your module name is "Test Module" you have the option to create your fields like "test_module_id", "test_module_title" or without it, like "id", "title".

mwhitneysdsu commented 9 years ago

I'm going to remove the 0.7.1 milestone from this one. At first, I thought this would be a simple feature to add, but as I consider actually coding it, it strikes me that, because the DB prefix is set on CI's Db_driver, there are a lot of potentially nasty things that could happen if the model isn't completely isolated with its own instance of the Db_driver. For example, if you change the dbprefix in the model, you can't access anything else in the database until you return the dbprefix to its previous value.

Almost every active record call uses the dbprefix (even if it's indirectly), so, while you can set it with $this->db->set_dbprefix($prefix);, you need to be careful that, while you're using the model, you aren't using some other part of the system that also accesses the database.

This may be as simple as overloading all of the methods in BF_Model which use $this->db like this:

class Prefix_Model extends BF_Model
{
    protected $newPrefix = 'new_';

    // ...
    public function find($id = '')
    {
        $oldPrefix = $this->db->dbprefix;
        $this->db->set_dbprefix($this->newPrefix);
        $result = parent::find($id);
        $this->db->set_dbprefix($oldPrefix);
        return $result;
    }
    // ...
    public function where($field = null, $value = null)
    {
        $oldPrefix = $this->db->dbprefix;
        $this->db->set_dbprefix($this->newPrefix);
        $result = parent::where($field, $value);
        $this->db->set_dbprefix($oldPrefix);
        return $result;
    }
    // ...
    // don't forget all of those database wrappers at the bottom...
}

However, even there you have to be careful. If you have log_user enabled, you're going to call $this->auth->user_id() in the middle of your delete (and/or insert/update) methods, and need to handle the dbprefix accordingly...

While it may be possible to create a model which extends BF_Model and can be used as a base model for models which use a different dbprefix, or it may be possible to make the Code Builder build models which do this, I don't think the current method of database interaction really supports it well enough for it to be a reasonably safe part of Bonfire's core.