Saas Using Mysql

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?

can anything do with CActiveRecord ??

i want to read the data of the current db user only…im saving the current username by a trigger …

any simple method?

any idea?

class Content extends CActiveRecord


    public function defaultScope()


        return array(





I’m not very far into my project but it uses the same SaaS strategy you refer to. I haven’t tried it yet but I believe you can declare the primary key in the view’s model AR class so you can use gii to generate the CRUD functionality:

public function primaryKey()


    return array('pk1');  // or whatever


Just remember that the view must only have columns from the original table only, and no columns (JOINED or otherwise) from any other tables.

My question though would be “why no primary keys”? This is very bad design. You are complicating your life tremendously without pk’s in every aspect of your development and maintenance.

I would seriously reconsider this design decision…

pk is there man…u misunderstood my qn.

im creating model for the "view" not table.

That is the point. thank u. will check this solution.