CREATE TABLE foo_base (
id INTEGER AUTO_INCREMENT PRIMARY KEY,
tenant VARCHAR(16),
foo VARCHAR(64),
bar VARCHAR(64)
);
CREATE VIEW foo AS
SELECT id, foo, bar
FROM foo_base
WHERE tenant = SUBSTRING_INDEX(USER(), '@', 1);
CREATE TRIGGER foo_base_tenant_trigger
BEFORE INSERT ON foo_base
FOR EACH ROW
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?
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…