Flourish PHP Unframework
This is an archived copy of the forum for reference purposes

Mapping Class to Database

posted by justbn 9 years ago

I've finally gotten around to trying to use the 'new' multiple database support in Flourish. However, I'm hitting a road block. Each time I use a class that I've mapped to the non-default database, flourish still attempts the query with the default database.

I'm sure I'm doing something wrong.

Does anyone have suggestions?

Here are my connection settings:

/*
 * Connection parameters for the STP Downloads DB (default)
*/
define('DOWNLOADS_DB_HOST','server_name');
define('DOWNLOADS_DB_USER','user_name');
define('DOWNLOADS_DB_PASS','user_password');
define('DOWNLOADS_DB','stp_downloads');
define('DOWNLOADS_DB_PORT',3397);


/*
 * Connection parameters for the SS7AUTO DB 
*/
define('SS7AUTO_DB_HOST','server_name');
define('SS7AUTO_DB_USER','user_name');
define('SS7AUTO_DB_PASS','user_password');
define('SS7AUTO_DB','ss7auto');
define('SS7AUTO_DB_PORT',3354);

Here is where I attach the databases and map the class to the database

// Consider the downloads_db to be the default
fORMDatabase::attach(
		$downloads_db = new fDatabase('mysql', DOWNLOADS_DB, DOWNLOADS_DB_USER, DOWNLOADS_DB_PASS, DOWNLOADS_DB_HOST, DOWNLOADS_DB_PORT),
		'default'
);

fORMDatabase::attach(
		$ss7auto_db = new fDatabase('mysql', SS7AUTO_DB, SS7AUTO_DB_USER, SS7AUTO_DB_PASS, SS7AUTO_DB_HOST, SS7AUTO_DB_PORT),
		'ss7auto_db'
);

// Tell flourish to use the ss7auto db for the access_code table
fORM::mapClassToDatabase('AccessCode', 'ss7auto_db');

Here is the class that should use the ss7auto_db

<?php

class AccessCode extends fActiveRecord
{
	/**
	 * Flourish required function
	 * @return 
	 */
	protected function configure()
	{
        // Note : This class is mapped to the database 'ss7auto' in constants.php
	}

    /**
     * Create a new entry in table downloads
     * @param String $access_code
     * @return Integer|BOOL - False on no affected rows
     */
    public static function insAccessCodes($access_code)
    {
        $sql = "insert into access_codes (code) values (%s)";
        
        try
        {
            $db = fORMDatabase::retrieve();
print_r($db);
            $sql = $db->escape($sql,$access_code);
echo "\\nSQL=$sql\\n";
            $result = $db->query($sql);
            
            if($result->countAffectedRows() !== 1)
            {
                return FALSE;
            }
            
            return $result->getAutoIncrementedValue();
            
        }   catch ( fSQLException $e ) { manage_error($e); }
            catch ( Exception $e ) { manage_error($e); }
    }
    
}
?>

The results of the print_r in the class are as follows :

fDatabase Object
(
    [cache:fDatabase:private] => 
    [cache_prefix:fDatabase:private] => 
    [connection:fDatabase:private] => mysqli Object
        (
            [affected_rows] => 10
            [client_info] => mysqlnd 5.0.5-dev - 081106 - $Revision: 289630 $
            [client_version] => 50005
            [connect_errno] => 0
            [connect_error] => 
            [errno] => 0
            [error] => 
            [field_count] => 4
            [host_info] => MySQL host info: server_name via TCP/IP
            [info] => 
            [insert_id] => 0
            [server_info] => 5.1.46-log
            [server_version] => 50146
            [sqlstate] => 00000
            [protocol_version] => 10
            [thread_id] => 151
            [warning_count] => 0
        )

    [database:fDatabase:private] => stp_downloads
    [debug:fDatabase:private] => 
    [error:fDatabase:private] => 
    [extension:protected] => mysqli
    [host:fDatabase:private] => nettest2
    [inside_transaction:fDatabase:private] => 
    [password:fDatabase:private] => user_password
    [port:fDatabase:private] => 3397
    [query_time:fDatabase:private] => 0.99228286743164
    [schema_info:protected] => Array
        (
        )

    [slow_query_threshold:fDatabase:private] => 
    [statement:fDatabase:private] => select cmd, clli, cmds_id, stps_id from cmds
            join cmds_to_cmd_groups as ctcg using (cmds_id)
            join cmd_groups as cg using (cmd_groups_id)
            join stps using (stps_id)
            join downloads_to_cmd_groups as dtcg using (cmd_groups_id)
            where downloads_id = 46
            order by ctcg.priority
    [translation:fDatabase:private] => 
    [type:fDatabase:private] => mysql
    [unbuffered_result:fDatabase:private] => 
    [username:fDatabase:private] => user_name
)

Does anyone see something I'm doing wrong? The print_r clearly shows the fORMDatabase::retrieve returned the 'default' database.

I even tried placing the mapClassToDatabase in the configure() function of the AccessCode class. That produced the same result.

When you are using multiple database with fORMDatabase::retrieve(), you need to pass the class name, and possibly the role. If you don't pass any parameters, you get the default database.

posted by wbond 9 years ago

LOL. Will, I was just posting to say I had "solved" the problem when I saw your response. I came up with the same solution.

However, for me, it begs a question. If I have to pass the class name in for the retrieve method, what's the point of the mapClassToDatabase()? I thought it was going to take care of the association so my retrieve calls retrieve the correct db.

posted by justbn 9 years ago

The real point of it is that it takes care of all of the fORMDatabase::retrieve() calls that happen throughout the ORM and plugins.

posted by wbond 9 years ago