[Solved] Application with multiple database

A place for users and developers of the Xataface to discuss and receive support.

[Solved] Application with multiple database

Postby silma » Tue Feb 08, 2011 6:51 am

Hello Steve & Xataface users,

I was wondering if i could use my application with different database without duplicating it.

What i'd like to do is having a page in my site, where i could select the database i need.

For exemple if i choose the link
"http://mysite.com/Canada" -> my application will use the Canada database
"http://mysite.com/USA" -> my application will use the USA database (same tables, different data)

That way, if i do some change on my application, i'll do it only once.

Thanks a lot for yours answers, and please excuse my english.
Last edited by silma on Thu Feb 02, 2012 9:19 am, edited 3 times in total.
silma
 
Posts: 87
Joined: Tue Apr 28, 2009 11:47 pm

Re: Application with multiple database

Postby shannah » Tue Feb 08, 2011 1:03 pm

Your use case is actually pretty easy. df_init() takes an optional third parameter which is an associative array of config options to override those of the conf.ini file.

So you could do something like this with your index.php file:
Code: Select all
$conf = parse_ini_file('conf.ini', true);
if ( urlIsCanadaURL() ){
    $conf['_database']['name'] = 'Canada';
} else {
    $conf['_database']['name'] = 'USA';
}
df_init(__FILE__, '/xataface', $conf)->display();
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm

Re: Application with multiple database

Postby silma » Wed Feb 09, 2011 12:03 am

I'll try that, thanks a lot for your help !
silma
 
Posts: 87
Joined: Tue Apr 28, 2009 11:47 pm

Re: Application with multiple database

Postby silma » Mon Jun 27, 2011 8:46 am

Hello Steve and everybody here,

I'm sorry to dig up this old thread, but i've got some difficulties to get my app working with two databases.

I've changed my index.php file as you indicated, and it's working as expected :
The login page display well, and the user are on the database they should be.

The trouble come with the next pages : the config "loses" the parameters, and log on the default database.

It seems a logical behaviour, but i'm stuck with it : how could i do to keep the same database all the session long ?

Thanks again !
silma
 
Posts: 87
Joined: Tue Apr 28, 2009 11:47 pm

Re: Application with multiple database

Postby shannah » Mon Jun 27, 2011 9:24 am

Why does the config lose the parameters? Is it because the user links to a different URL that doesn't include the country suffix?
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm

Re: Application with multiple database

Postby silma » Mon Jun 27, 2011 11:38 pm

Yes, it's exactly that : after the first click, the user do not come from Canada or USA anymore, he come from the app itself.

In fact, i realize i was not clear at all in my first post : What i'd like to achieve is having only one app directory and use it with 2 or more database.

It may be impossible : in that case i will just copy my app in another directory, but it would be easier for maintenance and new functions.

Thanks again Steve !
silma
 
Posts: 87
Joined: Tue Apr 28, 2009 11:47 pm

Re: Application with multiple database

Postby shannah » Tue Jun 28, 2011 1:23 am

There are a few ways to achieve multiple URLs for a single source base without having to copy the directory. (I would never recommend copying the directory for the same app unless there is a good reason).

Method #1: Use mod_rewrite
Method #2: Use symbolic links for the same directory
Method #3: Using different subdomains/domains for the same directory.

Example Using mod_rewrite:

In this example we allow users to access the application using example.com/fpa, example.com/iat, and example.com/math - but they all use the same source directory. The application is actually housed at / (i.e. the docroot).

.htaccess:
Code: Select all
RewriteEngine On
RewriteBase /
RewriteRule ^fpa(.*)$ $1 [E=DEPT:FPA,L]
RewriteRule ^iat(.*)$ $1 [E=DEPT:IAT,L]
RewriteRule ^math(.*)$ $1 [E=DEPT:MATH,L]


What we're doing here is stripping out the starting path and replacing it with an environment variable named 'DEPT'. The environment variable will be accessible to PHP as $_SERVER['REDIRECT_DEPT']. You can use this to set a cookie that is retained across all requests.

Using symbolic links is a little easier as you don't have to do any special cookies or environment variables. It actually acts as though the application is running in separate folders even though the source is all in the same folder.

Same with multiple domains... don't have to do anything special. You can detect the domain using $_SERVER['HTTP_HOST'].

-Steve
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm

Re: Application with multiple database

Postby silma » Tue Jun 28, 2011 3:18 am

Steve,

Thanks for this clear explanation, and for the time you pass answering me.

I'll opt for the subdomain method, i think.

I'll try it this afternoon.

Thanks again !

Edit : Finally, my Web Host did not accept multiple subdomain name pointing to the same directory, but the symlinks worked like a charm.

Thanks for your help Steve.
silma
 
Posts: 87
Joined: Tue Apr 28, 2009 11:47 pm

Re: Application with multiple database

Postby silma » Wed Jul 06, 2011 4:11 am

Hello Steve, hello everyone there,

I put my updated application online, and i experienced some crashes that never happened when i was testing in my locahost.
I'm using xataface 1.3rc.

The error that occured is "MySQL server has gone away". I've read the mysql manual on the subject, but it didn't help much..

Do you think it could come from the multiple database settings ?

It does not come from the hoster : I made a friend test on the same time, and he had not the error.
The second time (few hours late) , he had the error, and not me..

Thanks a lot for your help.

EDIT : That was an error from my hoster.

The application work well with multiple database although it's maybe a bit slower than before.
Thanks Steve.
silma
 
Posts: 87
Joined: Tue Apr 28, 2009 11:47 pm


Return to Xataface Users

Who is online

Users browsing this forum: No registered users and 25 guests

cron
Powered by Dataface
© 2005-2007 Steve Hannah All rights reserved