Yii Saas Approch

Hi everyone,

I would like to understand and build a SAAS with YII.

Now, I try to find as much information as I can before getting started.

But there’s still something I don’t get is how to store CODE content in the database.

We think about approaching the problem this way:


-Views: Template1 -> run code from DATABASE(TenantID)

-Views: Template2 -> run code from DATABASE(TenantID)

-Views: Template3 -> run code from DATABASE(TenantID)




After creating DB connection dynamically to the Tenant DB, we run the code from the DB to render the view template associated. Inside the template the code content will be pull out the DB and run.

Modules might be called as a service to run according to the tenant.

Now that being said, is this a good approach?

I mean to use the DB to push, pull, run code is this a good idea? or we can just use metadata to represent elements and then render them?

Can anyone point me to some algorithms, documentation, tutorials, examples of any kind about modern SAAS function I would really appreciate O0

Thank you for your time :D

I’m not sure what you’re doing here.

There are a few posts about using Yii in a SaaS environment but they all focus on using a shared DB instead of individual DBs per client. Without knowing the scope / size of your project its hard to say but if you have the opportunity to go shared it greatly simplifies your environment.

That said,

Why are you storing code to eval in the database? Is this user-changeable view code? Have you questioned the assumption that you need to store code? If this is about storing user-generated content a good approach is to use BBcode / markup or a WYSIWYG editor and store both raw and html versions in your table. Use HtmlPurifier to remove any security threats.

It’s not really clear to me either …

However, try this:

You want to look into OpenStack. ;)

As I read in this article: http://msdn.microsoft.com/en-us/library/aa479086.aspx

That one DB per Tenant is easy to start but may cost more with time in contrast with one DB for everyone. I still don’t follow that logic, and find 1DB/1tenant common sense as for a managing stand point…But I begin to ask myself if monitoring hundreds of separate DB is really more practical.

"size of your project" we already design website for clients the traditional way but rewrite the code again and again event with help of reusable modules and extension make little to no sense to us. And updating new versions of our modules for all clients… is a nightmare -.-"

We want to be able to host hundreds of corporate sites with different services according to needs.

I’m still trying to understand what’s the best approach to use pre-made templates… but be able to customize the code WITHOUT touching other client template.

Dynamic code + LESS is the best idea I came up with… please share your ideas, experiences and documentation… I try put this thing together :)

Thank you I will look at it :)

So what you describe is not really a SaaS ‘application’ (all customers get same version and content) but really customized website hosting?

You are basically offering a custom CMS for each customer?

Basically yes, a SAAS, BUT where I can have total control over the GUI by CSS/LESS and pictures.

Where 2 tenants can have the same layout but not the same "theme" if you will, which are created personally.


Greek restaurant and chinese restaurant.

Same layout, but totally not the same background and styles.

How can I solved this problem?

How we create a SaaS app using Yii?

How i implement this



CREATE TABLE foo_base (


    tenant VARCHAR(16),

    foo VARCHAR(64),

    bar VARCHAR(64)



    SELECT id, foo, bar

    FROM foo_base

    WHERE tenant = SUBSTRING_INDEX(USER(), '@', 1);

CREATE TRIGGER foo_base_tenant_trigger

    BEFORE INSERT ON foo_base


        SET new.tenant = SUBSTRING_INDEX(USER(), '@', 1);

i created model for the view foo, but cant create crud because that has no Pk. thats ok.

so i created model for foo_base and crud for the same. And when using the model Foo (not Foobase). its working perfectly according to DB user. its only fetching the current user’s value. but in Yii we are using lots of PK(primary key) based operations. like findByPk . so how i solve this?

That’s very bright! I never thought about that…

That resolve my problem about Shared/Separated DB’s…

But I still would like some advice about making templates, themes and GUI customized for each tenants.

[color="#0000FF"]Do I use Views as templates, and for each view I pull customized CODE from DB that I embed?[/color]


[color="#0000FF"]Do I use Views as templates, and for each view I pull metadata that will trigger a lists of widgets to be display…[/color]

I just came up with that so give me your best ideas…


Thank you for your time :D