Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » Connecting to different databases

Our old forums are still available as a read-only archive.

Moderators: martimiz, Sean, biapar, Willr, Ingo, simon_w

Page: 1
Go to End
Author Topic: 1159 Views
  • Nadee
    Avatar
    Community Member
    12 Posts

    Connecting to different databases Link to this post

    Hello,

    I have a requirement to connect to different databases depending on a given condition. The condition can be encoded as a part of the URL. I want to use the same CMS code, same template, same page types, etc.; but to connect to a different database.

    For example lets say I have a site called http://my_sample_site.com/ and I want SS to use the database 'fruits' whenever the URL http://my_sample_site.com/fruits/ or http://my_sample_site.com/?target=fruits is found. Likewise I may have another database called 'vegetables' whenever SS sees http://my_sample_site.com/vegetables/ or the other variant of the URL. Is this possible by any means?

    I can do this by using an HTTP Post variable but then permanent URLs cannot be given.

    Tried using Subsites module but no luck. It installs without issues but I don't get the Subsites drop-down on the CMS admin.

    Any help is greatly appreciated.

    Thanks.

  • Willr
    Avatar
    Forum Moderator
    5489 Posts

    Re: Connecting to different databases Link to this post

    the best way (and safest) is to use GET variables. As the URL parser has special order eg yoursite.com/ClassName/Action/ID so if you did yoursite.com/fruit/ it would first look for the Page with a url of fruit, then second a class called fruit. But you would probably want to set the DB on a bunch of situations.. So I would suggest have yoursite.com/?d=fruit then in the _config you can do if isset($_GET['d']) $database = ? etc.

  • Nadee
    Avatar
    Community Member
    12 Posts

    Re: Connecting to different databases Link to this post

    Hey Will,

    Thanks for the reply. Well, I tried doing that. I modified the .htaccess file to give me the database in the URL with

    /sapphire/main.php?db=$1&url=$2&%{QUERY_STRING} [L]

    .

    Then I added the line to _config.php in mysite to change the baseURL with

    Director::setBaseURL(<new base url>)

    .

    The issue is, it works if you type the URL manually, but SilverStripe doesn't make links to match the new base URL. So it gives a 'page not found' error.

    What can I do to remedy this?

    Thanks.

    1159 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.