Multisite approach

I’m trying to add multisite feature to contentbox. What I’m starting from is adding a new object Site.cfc in contentbox/model/system, adding a basemodel that has a many-to-one relationship to site. BaseModel.cfc has mappedsuperclass attribute.

I’ve added a site handler to edit sites. I’ve modified the installer to create a default site from hostname. It seems works. Now in the CBrequest interceptor I populate a prc variable with the site, and perform all my query passing this to all models. I want to know if it’s a good approach and if I can go on.

Here’s the repo if you want look: https://github.com/Tropicalista/ContentBox/tree/multisite

Francesco maybe we can do a screenshare. How about Friday at 2pm pacific?

Hi Luis,

unfortunately I don’t think is possible friday. What do you think about nex week?

What would be awesome is the ability to have multiple contentbox modules running, each mapped to a site. Need a new site, just spin up a module in an existing contentbox.Better yet let a customer do it via some request page.

Yes we can do next week.

Interesting Jonathan

Would be possible to switch database per request? Can we set a different database for each site? We could do something like:

<cffunction name="onRequestStart" returntype="void" output="false">
    <cfargument name="event" required="true">
    <cfquery name="q" datasource="#dsncore.getName()#">
        SELECT
            Datasource
        FROM
            Websites
        WHERE
            Domain = <cfqueryparam value="#cgi.http_host#" />
    </cfquery>

    <cfset rc.dataSource = q.Datasource />
</cffunction>

So that every site is self containing? this seems a more simple approach. Create a module to manage sites and then set the database based on the http_host. However I’m not sute if it’s possible…

Doesn’t Coldbox rely on the datasource settings in Application.cfc for its ORM stuff. You would need to do this at the pseudo constructor of the Application.cfc and not that way. Unless you’re not using ORM.

I think you’re right, but something like

<cffunction name="onRequestStart" returntype="void" output="false">
    <cfargument name="event" required="true">
    <cfquery name="q" datasource="#dsncore.getName()#">
        SELECT
            Datasource
        FROM
            Websites
        WHERE
            Domain = <cfqueryparam value="#cgi.http_host#" />
    </cfquery>

    <cfscript>
        this.datasource = q.Datasource;
    </cfscript>
</cffunction>

Could work? I’ve never used multiple datasource with ORM…

The this scope is not accessible from with certain methods of the Application.cfc, from my expereince with doing this exact same thing, is that you need to modify all this in the pseudo constructor. It is the only place one is permitted to make changes to the “this” scope.

You can’t do this with ORM. It has to be done on one data source.

why is that so Luis, I have use multiple datasources before. The trick is setting it up in the psuedo constructor with an application name change.

Hi Andrew,

Do you have a piece of sample code for thst?

Thanks,

George Murphy

I had it on my blog when it was up, but the sad fact is you can’t use ColdBox to do this. You have to modify this yourself. For example the two key pieces are the datesource in the ormsettings and the application name, in my case the URL was the indicator that defined which site to run.

So the urls would be as an example

site1.mydomain
site2.mydomain

In the pseudo section you do what ever you have to do to pull this information up, in what I did I had a table in the database that looked up the url and pulled up the datasource name and changed what I had to change. The application name was then changed so the site would be intialised as fresh if it had to be or kept the session info. Each site has to have a different name, so that the Datsource and ORM stuff can work.

I think anyone can figure out how to do this, it’s not hard. But the key as I said is that you can’t do this with Coldbox and has to be done with normal queries and not orm to pull the information up. Once the settings have been changed then the site runs as normal.

This approach will also only work if the databases are similar as well, for more complex differences entity wise you will need to think about what is common and what is not and adjust your entity creation to cater for that too, in my example I used mapped super class (is that right) so that any code that is special to the site would load that orm stuff up. But then you need to make sure that you adjust the cfclocations to cater for that as well. It was all blogged about a few years ago, how you could do this with examples. But it sure can be done, just not initially with ColdBox.

What I mean, is that I do not want to approach it from a multi-datasource point of view.

Luis Majano
CEO
Ortus Solutions, Corp
www.ortussolutions.com
P/F: 1-888-557-8057
Direct: (909) 248-3408

ColdBox Platform: http://www.coldbox.org

ContentBox Platform: http://www.gocontentbox.org
Linked In: http://www.linkedin.com/pub/3/731/483

Social: twitter.com/ortussolutions | twitter.com/coldbox | twitter.com/lmajano | twitter.com/gocontentbox

The database would have to be designed so that each installed module’s data would be independent.Maybe that module has some identifier so that the same module could be installed more than once.

I would leave this to a custom not core module :slight_smile:

Luis Majano
CEO
Ortus Solutions, Corp
www.ortussolutions.com
P/F: 1-888-557-8057
Direct: (909) 248-3408

ColdBox Platform: http://www.coldbox.org

ContentBox Platform: http://www.gocontentbox.org
Linked In: http://www.linkedin.com/pub/3/731/483

Social: twitter.com/ortussolutions | twitter.com/coldbox | twitter.com/lmajano | twitter.com/gocontentbox