tablename based on login username

Hi all,

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;

	}


}




Could you please explain a bit more…what you are trying to do.

have you created a module for this task and if yes then why you are creating a module…

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.

Why don’t you add a user_id field to your table and override CActiveRecord.defaultScope() in your model? Would be much saner and even scale better.

Thank you for your answer,

yes this model is a part of a user module:

protected/modules/user/models/my_model

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.

because I want that when a user login, for example user A can view and modify just table_1 and user B can view and modify table_2

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



Thanks for the suggestions, I solve using my ‘non standard’ approach like this:




public function tableName()

{

	$userid=Yii::app()->user->id;


	if($userid==99)

	

	{

		return Yii::app()->getModule('user')->table_1;

	}

	

	else


	{

		return Yii::app()->getModule('user')->table_2;

	}

}



interesting usage :lol:

may be used for such scenario(just a brainstorming :D ):

different user stored in different db ,rewrite this :




public function getDbConnection()

{

   $userid=Yii::app()->user->id;


	if($userid==99)

	

	{

		return Yii::app()->db1;

	}

	

	else


	{

		return Yii::app()->db2;

	}

}