[opinion request] module based table prefix

I always wanted to have module-based table prefix and I got the following code working.

However, I don’t feel it’s reliable it’s rather a hack or work-around, how do you think?

your opinion required!




<?php

class MyModule extends CWebModule

{

	private $m_app_db_table_prefix = null;

	const DB_TABLE_PREFIX = 'my_';


	public function init()

	{

		$this->setImport(array(

			'My.models.*',

			'My.components.*',

		));

	}


	public function beforeControllerAction($controller, $action)

	{

		if(parent::beforeControllerAction($controller, $action))

		{

                        //change prefix for module here

			$this->m_app_db_table_prefix = Yii::app()->db->tablePrefix;

			Yii::app()->db->tablePrefix = self::DB_TABLE_PREFIX;

			return true;

		}

		else

			return false;

	}

	

	public function afterControllerAction($controller, $action)

	{

		if(parent::afterControllerAction($controller, $action))

		{

                        //resume back whatever define in config file

			Yii::app()->db->tablePrefix = $this->m_app_db_table_prefix;

			return true;

		}

		else

			return false;

	}	

}

?>



in model:




public function tableName() {

	return '{{talename}}';

}



----- Begin rant –

No one is ever going to sell me on table prefixes lol, I dislike them immensely. Sometimes I think it’s like the old tab vs space debate.

Because using a table prefix is like saying: this data, it’s not like the other data. If this is the case why isn’t it in another database?

------ End Rant —

Anyway, technically I think it’s probably fine, just as long as you are aware of the limitations. The really annoying thing is any case where you are forced to write a custom query, you are going to need to do




$sql = "select * from {$tablePrefix}users";



Or as a placeholder replaced by CDbExpression or whatever, hardly the worst thing I’ve ever seen.

Anything not done in the action however is unaffected, so if you are using database driven sessions or whatever else, it’s going to happen in the non-prefixed tables. I can’t recall if filters get called before ‘beforeControllerAction’ or after, so if you use filters with a DB component, keep that in mind.

You rant means shit if there only can be one database, for instance on certain shared hosts.

It also doesn’t apply much with Yii as it assumes one database per application - at least ‘out of the box’.

Too true, I would probably do the same if someone paid me to. Was just a rant.

I deleted my original reply, I am feeling troll like.

my intention is trying to make each module as independent as possible while at the same time nicely tied up with other module, table prefix groups up your module tables; so i can drop the module from app to app a lot easily.

it seems even though yii provides AR, the magnet to use pure sql script seems pretty strong, however, in my opinion, if you have a script with 100 joins. there must be something wrong or some simple solution of table structure is there.

thanks @luke and @jacmoe for your input!