getherbert / herbert

The WordPress Plugin Framework:
http://getherbert.com/
632 stars 94 forks source link

Tables in Multi environment #83

Closed aucms closed 8 years ago

aucms commented 8 years ago

Hi ... i have a multi-wordpress installation, created my models, controllers and etc ... now when i switch to another blog the mysql tables take the prefixes from the multisite.

For example i have a table that is named member_coupon. When i change to another site it tries to lookup variables from the table blog_2_member_coupon which is the base for the multisite i am browsing.

How can i override this so that i am always using member_coupon and not with the default prefix wordpress sets?

EduardoOliveira commented 8 years ago

+1

EduardoOliveira commented 8 years ago

lacking a better solution i just found where the prefix is set:

    protected function registerEloquent()
    {
        global $wpdb;

        $capsule = new Capsule($this->app);

        $capsule->addConnection([
            'driver' => 'mysql',
            'host' => DB_HOST,
            'database' => DB_NAME,
            'username' => DB_USER,
            'password' => DB_PASSWORD,
            'charset' => DB_CHARSET,
            'collation' => DB_COLLATE ?: 'utf8_general_ci',
            'prefix' =>'wp_'
            //'prefix' => $wpdb->prefix
        ]);

        $capsule->setAsGlobal();
        $capsule->bootEloquent();
    }

vendor/getherbert/framework/Herbert/Framework/Providers/HerbertServiceProvider.php:125

aucms commented 8 years ago

Thank you @EduardoOliveira .. had not thought of this. Thank you for pointing this out. I can use this for now :) :+1:

imkebe commented 8 years ago

I little bit late but my simple solution is encapsulation.

use Illuminate\Database\Eloquent\Model;

class HerbertModel extends Model
{
    protected $prefix;

    public function __construct(array $attributes = [])
    {
        if(isset($this->prefix)){
            $this->table = $this->prefix.$this->table;
        } else {
global $wpdb;
            $this->table = $wpdb->prefix.$this->table;
        }
        parent::__construct($attributes);
    }
}

Now you need to keep in mind to use HerbertModel. U can just change "use" declaration to use alias for every model file like "Eloquent". Just define $prefix variable in model where you need custom prefix.

jeremyzahner commented 8 years ago

@imkebe Thanks for pointing out the (at least in my opinion) most valuable solution for this.

sjwdavies commented 7 years ago

@imkebe - Your solution doesn't appear to work. I've employed it using the Herbert Post class, by replicating the class and inserting your script.

However, my table always returns with the "wp{siteId}" prefix before the actual prefix - any ideas?