1. We apologize for the slow support at the moment. Hugh (cheesegrits) is scheduled for some major back surgery this month to repair two ruptured discs which are severely hampering his ability to work, and one of our stalwart community support members is on vacation for the month. Please bear with us.

Opening connections to other than the Joomla DB

Discussion in 'Standard Support' started by mccarty, Mar 18, 2010.

  1. mccarty New Member

    Level: Community
    Hi,

    I am storing the data for an application in a separate database and have a fabrik connection defined for it. I want to do a variety of operations to pull certain fields from the DB as part of some server-side validation, but of course I cannot just use "$db =& JFactory::getDBO();" to access it.

    Is there an existing method within the Fabrik environment to open a DB connection for other than the Joomla DB without having to retrieve the DB uid/password from the _fabrik_connections table (or elsewhere) myself and then calling mysql_connect() directly?

    (If this doesn't exist, it would be nice to have in the toolbox... :) )

    --Rick
  2. rob Administrator

    Level: Community
    PHP:

    $options        = array ( 'driver' => $driver, 'host' => $host, 'user' => $user, 'password' => $password, 'database' => $database, 'prefix' => $prefix );
     $dbs[$cn->id] =& JDatabase::getInstance( $options );
     
    to get an idea of the values for $driver etc you can load in J's default config values:

    PHP:

    $conf =& JFactory::getConfig();
          $host         = $conf->getValue('config.host');
          $user         = $conf->getValue('config.user');
          $password     = $conf->getValue('config.password');
          $database    = $conf->getValue('config.db');
          $prefix     = $conf->getValue('config.dbprefix');
          $driver     = $conf->getValue('config.dbtype');
          $debug         = $conf->getValue('config.debug');
     
  3. cheesegrits Support Gopher

    Level: Community
    So where does $cn->id come from?

    -- hugh
  4. mccarty New Member

    Level: Community
    I probably should have been a bit clearer with my question. I am already using JDatabase::getInstance - I just reflexively wrote mysql_connect() out of habit.

    The question really was how can I get at the connection parameters for a database stored in Fabrik's connection table? I know I can do a query myself (and I have, as a workaround), but it seems to me that it would be a good thing to be able to access it through a Fabrik-supplied interface instead.

    To be more specific - is there an internal Fabrik interface I can use instead of having to use the following code I wrote to handle this:

    function mwGetFabrikDBInfo($dbname)
    {
    $db = JFactory::getDBO();

    $query = "
    SELECT ".$db->nameQuote('host').",
    ".$db->nameQuote('user').",
    ".$db->nameQuote('password')."
    FROM ".$db->nameQuote('#__fabrik_connections')."
    WHERE ".$db->nameQuote('database')." = ".$db->quote($dbname).";
    ";

    $db->setQuery($query);
    $result = $db->loadObject();

    if ($db->getErrorNum()) {
    jexit('(mwGetFabrikDBInfo) Database query Error: ' . $db->getErrorMsg() );
    }

    return ($result);
    }

    function mwOpenFabrikDB($dbname, $host, $user, $password)
    {
    $parms = array();

    $parms['driver'] = 'mysql';
    $parms['host'] = $host;
    $parms['user'] = $user;
    $parms['password'] = $password;
    $parms['database'] = $dbname;
    $parms['prefix'] = '';
    $db =& JDatabase::getInstance($parms);

    if (JError::isError($db)) {
    jexit('(mwOpenFabrikDB) JDatabase::getInstance: Database open Error: ' . $db->toString() );
    }

    return ($db);
    }

    $dbinfo = mwGetFabrikDBInfo($dbname);
    $db = mwOpenFabrikDB($dbname, $dbinfo->host, $dbinfo->user, $dbinfo->password);

    Best regards,

    Rick
  5. amilks New Member

    Level: Community
    Friendly Bump...
  6. amilks New Member

    Level: Community
    Hugh, a friendly bump.... We talked about this on skype
  7. cheesegrits Support Gopher

    Level: Community
    I'm without power at my house at the moment (lightning storm last night), sitting at a coffee shop. So I don't have access to my test server to look at the code. Bump this again tomorrow.

    -- hugh
  8. amilks New Member

    Level: Community
    LOL! :eek: Will do
  9. cheesegrits Support Gopher

    Level: Community
    Testing some code, will hopefully get back to you today on this one.

    -- hugh
  10. cheesegrits Support Gopher

    Level: Community
    Try this:

    PHP:
    $cn = JModel::getInstance('Connection', 'FabrikModel');
    $cn->setId(2);
    $db =& $cn->getDb();
    -- hugh
  11. amilks New Member

    Level: Community
    Here is the exact code I am using:

    User_Ajax.php

    Code (text):
    class userAjax {
    function getStudent(){
    $cn = JModel::getInstance('Connection', 'FabrikModel');
    $cn->setId(2);
    $db =& $cn->getDb();
    $checkId = JRequest::getVar('idField', '');
    $query = "SELECT * from jos_fbkstudents WHERE student_id = '$checkId' LIMIT 1";
    $db->setQuery($query);
    $results = $db->loadObjectList();
    echo json_encode($results);
    }
    }
    3.js

    Code (text):
    function doCalc(){
    var total = 0;
    var type = form_3.formElements.get('jos_signups___signup_type').getValue();
    var question = form_3.formElements.get('jos_signups___current_student_q').getValue(); //question
    var url = 'index.php?option=com_fabrik&format=raw&controller=plugin&c=plugin&task=userAjax';
    var idField = $('jos_signups___student_id').getValue(); //id field
    var originalId = $('jos_signups___student_id'); //id field
    var verifyIdField = $('jos_signups___verify_id'); //verify id field
    var updateEmail = $('jos_signups___email'); //student email
    var updateName = $('jos_signups___name'); //student name
    var updateAddress1 = $('jos_signups___address1'); //street address
    var updateAddress2 = $('jos_signups___address2'); //street address 2
    var updateCity = $('jos_signups___city'); //city
    var updateState = $('jos_signups___state'); //state
    var updateZip = $('jos_signups___zip'); //zip
    var updateCountry = $('jos_signups___country'); //country
    var updatePrePhone = $('jos_signups___pre_phone'); //preferred phone
    var updateAltPhone = $('jos_signups___alt_phone') //alternate phone
    var updateFax = $('jos_signups___fax'); //fax
    var date1 = $('jos_signups___date1');
    var specialDate = form_3.formElements.get('jos_signups___datespecial').getValue();

    new Ajax(url,{
    data:{method:'getStudent',
    'idField':idField},

    onComplete:function(r){
    var student_info = Json.decode(r);
    verifyIdField.value = student_info[0].student_id;
    updateEmail.value = student_info[0].email;
    updateName.value = student_info[0].name;
    updateAddress1.value = student_info[0].address1;
    updateAddress2.value = student_info[0].address2;
    updateCity.value = student_info[0].city;
    updateState.value = student_info[0].state;
    updateZip.value = student_info[0].zip;
    updateCountry.value = student_info[0].country;
    updatePrePhone.value = student_info[0].pre_phone;
    updateAltPhone.value = student_info[0].alt_phone;
    updateFax.value = student_info[0].fax;
    if (type == "1") {
    $('jos_signups___cost').value = 400;  
    $('group5').hide();
    $('jos_signups___group_name').hide();
    $('fb_el_jos_signups___group_name_text').hide();
    if (question == "2" && originalId.value == verifyIdField.getValue() && originalId.value != '') {
    $('jos_signups___cost').value = 199;
    $('fb_el_jos_signups___signup_type_text').hide();
    $('jos_signups___paypal_desc').value = 'Total Terminology Course, ' + specialDate + ', student id ' + originalId.getValue();
    date1.value = specialDate;
    $('jos_signups___datespecial').show();
    $('fb_el_jos_signups___datespecial_text').show();
    $('jos_signups___date1').hide();
    $('fb_el_jos_signups___date1_text').hide();
    $('jos_signups___date2').hide();
    $('fb_el_jos_signups___date2_text').hide();
    $('jos_signups___date3').hide();
    $('fb_el_jos_signups___date3_text').hide();
    $('jos_signups___signup_type').hide();
    $('fb_el_jos_signups___signup_type_text').hide();
    $('jos_signups___signup_type_text').show();
    $('fb_el_jos_signups___signup_type_text_text').show();
    }
    else {  
    $('jos_signups___cost').value = 400;
    $('jos_signups___signup_type').show();
    $('fb_el_jos_signups___signup_type_text').show();
    $('jos_signups___paypal_desc').value = 'Total Terminology Course, ' + date1.getValue();
    $('jos_signups___datespecial').hide();
    $('fb_el_jos_signups___datespecial_text').hide();
    $('jos_signups___date1').show();
    $('fb_el_jos_signups___date1_text').show();
    $('jos_signups___date2').show();
    $('fb_el_jos_signups___date2_text').show();
    $('jos_signups___date3').show();
    $('fb_el_jos_signups___date3_text').show();
    $('jos_signups___signup_type').show();
    $('fb_el_jos_signups___signup_type_text').show();
    $('jos_signups___signup_type_text').hide();
    $('fb_el_jos_signups___signup_type_text_text').hide();
    }
    }
    else if (type == "2") {
    $('group5').show();
    $('jos_signups___group_name').show();
    $('fb_el_jos_signups___group_name_text').show();
    $('group5').getElements('input[id^=jos_signups___group_subcost]').each(
       function(i){
        total += i.getValue().toInt();
         $('jos_signups___cost').value = total;
         if ($('jos_signups___cost').value <= 1200) {
           $('jos_signups___cost').value = 1200;
           }
         else {
         $('jos_signups___cost').value = total;
         }
       })}
    }}).request();};
    And here is the error I am getting:

    Code (text):
    missing ) in parenthetical
    http://website.com/components/com_fabrik/libs/mootools1.2/mootools-1.2.js
    Line 255
    Hope this helps. Maybe I am just doing something a LITTLE wrong.
    1 person likes this.

Share This Page