separate database for each user

Hello, I have a CMS with single backend and frontend. I need to remake it so that every user would have his own page with his own data in it. That is - a website with multiple Front-Ends.

For example www.mycms.com/<USERNAME>

I decided to have a different database for each user. What I need to know is … when somebody requests for a particular user page, I should change the database of Yii::app() to the corresponding user_database. How is it possible to do? I think that I should do it in a controller?

I am not the best one for answer this question.

I can advice you this solution: create a file component/ApplicationBehaviour.php




<?php


class ApplicationBehavior extends CBehavior

{

	/**

	 * Declares events and the event handler methods

	 * See yii documentation on behaviour

	 */

	public function events()

	{

		return array_merge(parent::events(), array(

			'onBeginRequest'=>'beginRequest',

		));

	}


	/**

	 * Load configuration that cannot be put in config/main

	 */

	public function beginRequest()

	{

		$this->owner->db= new CDbConnection([...]);

	}

}



In config/main add this line:




	'behaviors'=>array(

		'databaseConfig'=>array(

			'class'=>'ApplicationBehavior'

		)

	),



In this way, the code of ApplicationBehavior::beginRequest() will be executed before any controller action.

Give a look at the doc about details about how to set up the connection.

I think, that’s not so good idea to store data of users in separate databases.

Well could you somehow motivate your position on this? What would be the other way to distinguish users?

Each user has his own database, which holds data about user menus and content. By capturing url ‘www.mycms.com/johns_page’ the script finds out that it needs to load data from ‘johns_page_db’ database. This is quite clear way of the code and script to work. Simple. Now how to put all that data in the same database without loosing simplicity and clearness?

I guess you have the same tables for each database? That’s why you could add user_id column to each content table (menus etc). But I don’t know your application, I guess if you build like a hosting-application were users are able to build up their own site, your approach can make more sense (so users will be able to backup their own database for example).

www.mycms.com/my-name/

where my-name can be unique URL identifier of each user (and user has its own id (unique too)). then, you can select user by his URL identidier and show his data.

imagine what happens if you want to change user name (my-name to my-another-name). copy database to another or what?

i don’t know what your application is going to be. i don’t know your scheme. maybe, will be better store it separately.

:wink:

and another question is how many users do you have, which type of databases, etc etc …

Y!!,

Yes, you are right, I have the same tables for each database. In fact, its an exact copy of table structure. And yes, users build up their own site, with their own content and files.

Now I am trying to solve error "Property "CWebApplication.db" is read only.". It happens when I try to create new connection to other database.

Yes, this is a very good question "what happens if you want to change user name". I think, that maybe it will be possible somehow to rename the database of a user and the directory of a user also (each user has its own directory. The name of the directory is the same unique user url). I dont know a lot about databases, so maybe I am missing something and it will be more difficult than I expect?