Results 1 to 6 of 6

Thread: Logging in DBManager or MssqlManager

  1. #1
    eggsurplus's Avatar
    eggsurplus is offline Sugar Community Member
    Join Date
    Dec 2005
    Location
    Minnesota
    Posts
    2,343

    Default Logging in DBManager or MssqlManager

    I'm trying to do some logging in DBManager->getInstance and MssqlManager->connect and I'm not having much luck. Here's the code that I'm using:
    PHP Code:
    require_once('log4php/LoggerManager.php');   
    $GLOBALS['log'] = LoggerManager::getLogger('SugarCRM');
    $GLOBALS['log']->fatal('I am right here'); 
    I even emptied $dbinstances to force a new connection before calling DBManager::getInstance with no results in the error log. Any ideas?

  2. #2
    eggsurplus's Avatar
    eggsurplus is offline Sugar Community Member
    Join Date
    Dec 2005
    Location
    Minnesota
    Posts
    2,343

    Default Re: Logging in DBManager or MssqlManager

    I'd delete this thread but I can't. I was writing code against a copy of the file instead of the live file. Thus the changes not showing up. How about I check which file I'm editing on. Please excuse my idiocy.

  3. #3
    andydreisch's Avatar
    andydreisch is offline Sugar Team Member
    Join Date
    Apr 2005
    Location
    San Jose
    Posts
    2,080

    Default Re: Logging in DBManager or MssqlManager

    eggsurplus, you're entitled to the occasional "oops".

    Considering all your input recently to Sugarland you're more than entitled.

    Andy
    Andy Dreisch
    Vice President, Online Team


    Check out our Podcasts!
    Sugar University for training
    Sugar Wiki for developer and user help
    SugarForge for modules, themes, lang packs
    SugarExchange for production-ready extensions
    Enter/view bugs via the Sugar bug tracker

  4. #4
    eggsurplus's Avatar
    eggsurplus is offline Sugar Community Member
    Join Date
    Dec 2005
    Location
    Minnesota
    Posts
    2,343

    Default Re: Logging in DBManager or MssqlManager

    Thanks Andy, I appreciate it.

    I do have one issue along these lines though. I'm trying to set up a database connection to a different database on a different server using the classes provided in Sugar already (I currently have a separate class to connect to a different db but would like to simplify it and keep the classes the same). To set up multiple $dbinstances I changed DBManager getInstance to look for the configuration based on the instance name:
    PHP Code:
    if(empty($instanceName)) $instanceName 'db';
    $config $sugar_config[$instanceName.'config']; 
    This works for creating another connection and the new connection does work properly but the old 'db' instance for Sugar get's screwed up a bit. It's DBHelper object has the new connection's DBManager object instead of it's own. Here's the $dbinstances array after adding a second database instance:
    PHP Code:
    Array ( [db] => MssqlManager Object ( [dbType] => mssql 
    [helper] => MssqlHelper Object ( [db] => MssqlManager Object ( [dbType] => mssql 
    [helper] => MssqlHelper Object *RECURSION
    [
    tableName] => [database] =>Resource id #297
    [dieOnError] => [encode] => [query_time] => [lastmysqlrow] => -[last_error] => [lastResult] => Array ( ) [count_id] => [references] => ) [bean] => ) 
    [
    tableName] => [database] => Resource id #152
     
    [dieOnError] => [encode] => [query_time] => 0.007982 [lastmysqlrow] => -[last_error] => [lastResult] => Array ( ) [count_id] => [references] => 37 [lastsql] => SELECT contacts.* , contacts_cstm.* FROM contacts LEFT JOIN contacts_cstm ON contacts.id contacts_cstm.id_c WHERE contacts.id '37ec27ad-052c-fd27-26d6-4397652e3bd1' 

    [
    cpu] => MssqlManager Object ( [dbType] => mssql 
    [helper] => MssqlHelper Object ( [db] => MssqlManager Object *RECURSION* [bean] => )
     [
    tableName] => [database] => Resource id #297 [dieOnError] => [encode] => 1 [query_time] => 0 [lastmysqlrow] => -1 [last_error] => [lastResult] => Array ( ) [count_id] => [references] => 0 ) ) 
    cpu is the 2nd database instance. Resource id #152 is for the Sugar db and Resource #297 is the Cpu db. You can see in the array above that #297 is in the MssqlHelperObject for the Sugar db instance. Any ideas to get this object to not get overwritten?

    Thanks,
    Jason

  5. #5
    eggsurplus's Avatar
    eggsurplus is offline Sugar Community Member
    Join Date
    Dec 2005
    Location
    Minnesota
    Posts
    2,343

    Default Re: Logging in DBManager or MssqlManager

    After resetting the helper db to the original helper the properties are now correct. But I still get an Invalid object name 'accounts' when I try to use the Sugar $db after the new connection.
    PHP Code:
    $temp_helper $db->helper->db;
    $cpu_db = & DBManager::getInstance("cpu");    
    $db->helper->db $temp_helper;

    //make cpu_db query...returns correctly
    $cpu_db->query("select * from mytable");

    //make db query...invalid object name...must be using the new connection somehow.
    $db->query("select * from accounts"); 
    Last edited by eggsurplus; 2007-06-05 at 05:56 PM.

  6. #6
    eggsurplus's Avatar
    eggsurplus is offline Sugar Community Member
    Join Date
    Dec 2005
    Location
    Minnesota
    Posts
    2,343

    Default Re: Logging in DBManager or MssqlManager

    Figured it out! Here's how to connect to an external database:
    1. Edit DBManager getInstance. Change $instanceName = 'db'; to:
    if(empty($instanceName)) $instanceName = 'db';
    $config = $sugar_config[$instanceName.'config'];

    2. Edit DBManager disconnectAll. Change $dbinstances['db']->disconnect(); to:
    foreach($dbinstances as $instanceName => $instance) {
    $instance->disconnect();
    }

    3. Edit MysqlManager and/or MssqlManager and change all references to mysql_query or mssql_query by adding the link identifier as the second param (this way it knows which database connection to use):

    @mysql_query($sql,$this->database);

    4. Add your new database configuration to the config.php file. In this example I'll use mydb:
    'mydbconfig' =>
    array (
    'db_host_name' => 'localhost',
    'db_host_instance' => '',
    'db_user_name' => 'myuser',
    'db_password' => 'mypassword',
    'db_name' => 'mydb',
    'db_type' => 'mysql',
    ),

    5. Make your connections!

    require_once('include/database/DBManager.php');
    $mydb = & DBManager::getInstance("mydb");

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Logging problem...
    By Sabicho in forum Developer Help
    Replies: 5
    Last Post: 2007-02-09, 05:39 PM
  2. Logging problem...
    By Sabicho in forum General Discussion
    Replies: 0
    Last Post: 2007-02-09, 03:06 PM
  3. Logging is not working
    By Tempo in forum Developer Help
    Replies: 0
    Last Post: 2005-12-20, 10:07 PM
  4. Help w/ Sugar's logging mechanism
    By Tempo in forum General Discussion
    Replies: 0
    Last Post: 2005-12-01, 08:34 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •