Search

Does anyone know of any way to alter the $this->_Parent->Configuration database values before the database connection is established? I’m almost done developing an extension that will ease development by allowing development/staging/live config values to be set.

I’ve been looking for a delegate for this but aren’t sure where the db connection happens in relation to the available delegates.

Approx. line 81 of /lib/core/class.symphony.php:

$this->initialiseDatabase();

The problem is there needs to be a database connection for delegates to work (ExtensionManager::notifyMembers()), hence why there isn’t one before that.

I once built an extension using an intranet database. I initialised the database using the following method (using additional configuration params in config.php , structured like the main database config details):

public function initialiseIntranetDatabase(){
    $error = NULL;

    $details = $this->_Parent->Configuration->get('intranet-database');

    $driver = $details['driver'];
    $driver_filename = TOOLKIT . '/class.' . $driver . '.php';

    if(!is_file($driver_filename)){
        trigger_error("Could not find database driver '<code>$driver</code>'", E_USER_ERROR);
        return false;
    }

    require_once($driver_filename);

    $this->IntranetDatabase = new $driver;

    if(!$this->IntranetDatabase->connect($details['host'], $details['user'], $details['password'], $details['port'])) return false;
    if(!$this->IntranetDatabase->select($details['db'])) return false;
    if(!$this->IntranetDatabase->isConnected()) return false;

    // // not needed
    // $this->IntranetDatabase->setPrefix($details['sym_prefix']);

    if($details['runtime_character_set_alter'] == '1'){
        $this->IntranetDatabase->setCharacterEncoding($details['character_encoding']);
        $this->IntranetDatabase->setCharacterSet($details['character_set']);
    }

    // // not needed
    // if($this->Configuration->get('force_query_caching', 'database') == 'off') $this->IntranetDatabase->disableCaching();
    // elseif($this->Configuration->get('force_query_caching', 'database') == 'on') $this->IntranetDatabase->enableCaching();

    return true;
}

You might want to throw custom errors:

public function throwCustomError(){
    $this->_Parent->customError(E_USER_ERROR, 'Problems!', 'No database connection.');
}

Then, in another function, you may initialise the DB like so:

if(!$this->initialiseIntranetDatabase()){
    $this->throwCustomError();
}

Now, for example, receive some POST data and insert them into the database:

// get post data
if(isset($_POST['fields'])) $fields = $_POST['fields'];

// sanitize values
foreach ($fields as $item => $value) {
    $fields[$item] = General::sanitize($value);
}

... (do more stuff, e.g. find insert_id) ...

// execute insert
if(!$this->IntranetDatabase->insert($fields, 'insert_id')){
    $this->throwCustomError();
    return false;
}

I hope this helps.

Thanks for the responses folks - michael-e’s method of using a seperate $driver to connect to the intranet is really interesting for working with multiple dbs but what I needed was a way to override the default db connection details throughout the core, and as Alistair says the connection has already been established with the config.php values. This would throw an error on a different system.

Looks like I’ll have to stick to the manual way of doing this for now rather than packaging it up in an extension to release. If I do find a workaround then I’ll update.

Thanks for the input!

Create an account or sign in to comment.

Symphony • Open Source XSLT CMS

Server Requirements

  • PHP 5.3-5.6 or 7.0-7.3
  • PHP's LibXML module, with the XSLT extension enabled (--with-xsl)
  • MySQL 5.5 or above
  • An Apache or Litespeed webserver
  • Apache's mod_rewrite module or equivalent

Compatible Hosts

Sign in

Login details