Multiple databases

49 views
Skip to first unread message

Martin

unread,
Sep 28, 2006, 3:41:55 AM9/28/06
to Php Object Generator
I want to create objects from two or more different databases. What are
the best practices for doing this with POG generated code? In the
standard scenario, there is only one configuration file, pointing to
one single database. But if I want to connect to more than one
database, what would be the best way to go about doing that?

Thanks!

/Martin

Joel

unread,
Sep 28, 2006, 10:55:12 AM9/28/06
to Php Object Generator
I personally never encountered this scenario, but if I were to do it,
here's how I would go about:

I would modify the constructor of the database object in
class.database.php so that it looks something like this:

function DatabaseConnection($whichDatabase)
{
if ($whichDatabase == <first database>)
{
//instantiate connection 1
}
else
{
//instantiate connection 2
}
}

Then go in each the object that you want to store in db1 and make sure
that when new Database() is called, it's called as new
Database(<database 1>)

Same for db 2.

Hope this helps,
Joel

biafuo

unread,
Sep 16, 2013, 11:49:44 AM9/16/13
to php-object...@googlegroups.com, Php Object Generator
Hi, i'm interested to resolve this question. 
I need to have different connection with different database.
I give you an example-scenario:

//load class database and user, and the file configuration with father-database
    include_once 'configuration.php';
    include_once 'objects/class.database.php';
    include_once 'objects/class.user.php';

//get by session the userId that is logged to find how database was associated
    $user_loggedId = unserialize($_SESSION['$user_logged']);
    $user_temp = new User(); //oggetto utente
    $user = $user_temp ->Get($user_loggedId); 
    $database_name = $user->database_name;

//now change name_db in the array configuration
$configuration['db'] = $database_name;

//now reload a class
include_once 'objects/class.dtpl_anno.php';

//a the and i'm load a list of year, that in specified in the new user-database
$year_temp = new Year();
$years = $year_temp ->GetList();

the problem is that the connection doesn't change....can you help me?

thank's

Mario

unread,
Sep 16, 2013, 5:57:34 PM9/16/13
to php-object...@googlegroups.com

Look this example:
https://groups.google.com/forum/m/#!msg/php-object-generator/8tzpIsVMSKI/L-00ivXDWJ0J

If you need more information I can give you more example of usage

--
You received this message because you are subscribed to the Google Groups "Php Object Generator" group.
To unsubscribe from this group and stop receiving emails from it, send an email to php-object-gener...@googlegroups.com.
To post to this group, send email to php-object...@googlegroups.com.
Visit this group at http://groups.google.com/group/php-object-generator.
For more options, visit https://groups.google.com/groups/opt_out.

biafuo

unread,
Sep 17, 2013, 4:36:13 AM9/17/13
to php-object...@googlegroups.com
thaks for the answer.
I have already checked that post but does not work.

Anyhow i want tell you what i do:
 
1) I modified the file class.database.php  with two method reset anche connect (like the example)

2) The usage in the file is:

//load class database and user, and the file configuration with father-database
    include_once 'configuration.php';
    include_once 'objects/class.database.php';
    include_once 'objects/class.user.php';

//get by session the userId that is logged to find how database was associated
    $user_loggedId = unserialize($_SESSION['$user_logged']);
    $user_temp = new User(); //oggetto utente
    $user = $user_temp ->Get($user_loggedId); 
    $database_name = $user->database_name;

//change connection with new method
Database::Reset();

//now change name_db in the array configuration
$configuration['db'] = $database_name;

//now reload a class
include_once 'objects/class.dtpl_anno.php';

//a the and i'm load a list of year, that in specified in the new user-database
$year_temp = new Year();
$years = $year_temp ->GetList();

in this case i have a big alert:
 Fatal error: Access to undeclared static property: Database::$database -->>objects/class.database.php

I hope that you can help me
thank a lot

Mario

unread,
Sep 17, 2013, 5:02:58 AM9/17/13
to php-object...@googlegroups.com

Marco, do you need more help?
Please don't hesitate to contact us for further information.

biafuo

unread,
Sep 17, 2013, 5:50:47 AM9/17/13
to php-object...@googlegroups.com
Hi, yes i need more help...i have write a long post but at the end was not published!
I don't know why! 
Anyhow.....i tel you a problem:

1) thanks for the link but I had already read through the post and did not give me help

2) Now you paste here what I've done:

I modified the file "class.database.php" as shown in the link and so we have the method connect() anche reset().
So i show you the usage in the generic file:

//load class database and user, and the file configuration with father-database
    include_once 'configuration.php';
    include_once 'objects/class.database.php';
    include_once 'objects/class.user.php';

//get by session the userId that is logged to find how database was associated
    $user_loggedId = unserialize($_SESSION['$user_logged']);
    $user_temp = new User(); //oggetto utente
    $user = $user_temp ->Get($user_loggedId); 
    $database_name = $user->database_name;

//change database with a new method
Database::Reset();

//now change name_db in the array configuration
$configuration['db'] = $database_name;

//now reload a class
include_once 'objects/class.dtpl_anno.php';

//a the and i'm load a list of year, that in specified in the new user-database
$year_temp = new Year();
$years = $year_temp ->GetList();
in this case i have a big alert:
 Fatal error: Access to undeclared static property: Database::$database

....what could be the problem? or you can show me other examples please?
Thank you so much

Marco Biavati

unread,
Sep 17, 2013, 11:14:52 AM9/17/13
to php-object...@googlegroups.com
Hi, yes i need more help...i have write a long post in a google groups but at the end was not published!

I don't know why! 

Anyhow.....i tell a problem:

1) thanks for the link but I had already read through the post and did not give me help

2) Now you paste here what I've done:

I modified the file "class.database.php" as shown in the link, so we have the method connect() anche reset().
So i show you the usage in the generic file:

//load class database and user, and the file configuration with father-database
    include_once 'configuration.php';
    include_once 'objects/class.database.php';
    include_once 'objects/class.user.php';

//get by session the userId that is logged to find how database was associated
    $user_loggedId = unserialize($_SESSION['$user_loggedId']);
    $user_temp = new User(); //oggetto utente
    $user = $user_temp ->Get($user_loggedId); 
    $database_name = $user->database_name;

//change database with a new method
Database::Reset();

//now change name_db in the array configuration
$configuration['db'] = $database_name;

//now reload a class
include_once 'objects/class.dtpl_anno.php';

//a the and i'm load a list of year, that in specified in the new user-database
$year_temp = new Year();
$years = $year_temp ->GetList();

in this case i have a big alert:
 Fatal error: Access to undeclared static property: Database::$database

....what could be the problem? 
or you can show me other examples please?

Thank you so much


2013/9/16 Mario <lok...@gmail.com>

Mario

unread,
Sep 17, 2013, 12:51:08 PM9/17/13
to php-object-generator
use this modified version of database class:
http://pastebin.com/Zf24TsQu

it has more debug information.
Try to read it and to understand it.
Also add this to your configuration.php (if you want to, this step is optional):

$configuration['enconding'] = 'utf8';


Now your code:


require_once 'configuration.php';
require_once 'objects/class.database.php';
require_once 'objects/class.user.php';
require_once 'objects/class.dtpl_anno.php'; //you can move the require to the first part of the script

//get by session the userId that is logged to find how database was associated
    $user_loggedId = unserialize($_SESSION['$user_loggedId']);

    $user_temp = new User(); //oggetto utente
    $user = $user_temp ->Get($user_loggedId); 

   error_log('user_loggedId is now: '.$user_loggedId);

//check if it has been loaded
if ($user->userId == $user_loggedId)
{
    $database_name = $user->database_name;
//change database with a new method
    Database::Reset();

//now change name_db in the array configuration
    $configuration['db'] = $database_name;


   error_log('Database is now: '.$database_name);

//a the and i'm load a list of year, that in specified in the new user-database
    $year_temp = new Year();
    $years = $year_temp ->GetList();


   foreach($years as $year)
   {
      echo var_export($year,true).'<br />'; //or whatever 
   }

}



I hope this works.
Have a nice day!


Loksly

unread,
Sep 18, 2013, 3:19:05 AM9/18/13
to php-object...@googlegroups.com
Sorry, I meant:

$configuration['encoding'] = 'utf8';

biafuo

unread,
Sep 18, 2013, 10:49:24 AM9/18/13
to php-object...@googlegroups.com

fantastic.........it's work!!

thank you!!!!
Reply all
Reply to author
Forward
0 new messages