Im new of Yii, Ive a lot of tables with same structure and a user table with different users,
the idea is to use just one model for all the tables,
what I want to do is finding a way to change tablename in the model based on login username,
so a particular user can access to a certain table.
This is my tests but it doesnt work, when I try to login with a user different to 7, the user see always the
table_1, what I dont understand is that if I put in the if condition an id of a user that doesnt exist (999) in my user table, then the else condition is true…
Does this approach have sense?
public function tableName()
{
if ($userid=User::model()->exists('id=7'))
{
return Yii::app()->getModule('user')->table_1;
}
else
{
return Yii::app()->getModule('user')->table_2;
}
}
Why do you want to have a seperate table for each user ? That makes absolutely no sense…
Please explain in detail what you want to achive with that. And I agree with jayant that without more informations/code the module call is very confusing.
If the table columns of the user tables are always the same (I bet they are) you should simply save everthing about the users in ONE table so you can access the informations also with ONE model via the username/password combination or primary key.
You have to follow Database normalization rules to get a well designed database.
What I was not able to do is to find a simple way to assign to a user a table using just one model,
in the controller I’ve already defined the accessRules, the problems is to find a way to dynamically change the public function tableName based on the username that login.
In my opinion you don’t understand the advantage of database normalization.
To achive your goal that user A can view/modify only table_1 data and user B only table_2 data you can simply save everthing in one table with an user_id like Da:Sourcerer mentioned.
/* table for storing user data.
user id 1 => User A
user id 999 => User B
...
*/
id user_id your_column1 your_column2 ....
1 1 data for UserA data for userA
2 999 data for userB data for userB