![]() |
Active Record dbprefix... sort of - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Active Record dbprefix... sort of (/showthread.php?tid=5250) |
Active Record dbprefix... sort of - El Forum - 01-14-2008 [eluser]tonanbarbarian[/eluser] Coming from a background of developing for Mambo and Joomla! CMSes for the last few years I find myself liking the way that when writing queries for these frameworks (if you can call them frameworks) you could prefix your table names in the query and it would be automatically converted. So you would write something like this Code: SELECT * from #__users Code: SELECT * FROM mos_users Now I know that if I am using active record I can set the config to Code: $db['default']['dbprefix'] = 'mos_'; There are a couple of issues I have with the way it is being done is CI Firstly there is some complex regex having to be done to try to find situations where it cannot detect the table name correctly or where it has to prefix the table names in where clauses etc If CI used a similar method to that detailed above it would be much easier to code for. Secondly unlike the above example I find myself wanting to be able to replace multiple prefixes. The thing is I am looking to build some code that can make my apps work in any integrated environment. If any other PHP web app needs some functionality I want to be able to code something in CI to handle it. Lets say you want a Refer Friend system on a site (not what I am actually working on), then the CI code should be able to integrate into the Session and User information for the existing PHP app and work with it by using some simple libraries that are loaded for the existing app. But to keep things seperated a bit I want to be able to store data in my own tables with their own prefix. So the default app might be using mus_user as its table prefix but I want my table to be my_referers The issue with the active record dbprefix is that there is only one of them. I have tried to look into ways to extend the db classes but cannot see any way to do it because of the way they are loaded, with the exception of creating my own database loader (which I may have to do) But I do think it might be a nice feature in future version of CI to be able to specify an array of replacements something like this config/database.php Code: $db['default']['dbreplace'] = array('#1#_'=>'mos_','#2#_'=>'my_','#3#_'=>'os_'); This way you would create a query something like follows Code: $this->db->select('#1#_users.username, #2#_products.name, #3#_orders.amount'); Code: SELECT mos_users.username, my_products.name, os_orders.amount In the mean time I guess I will write my own database loader so that I can extend the active record class and provide this feature myself Active Record dbprefix... sort of - El Forum - 01-14-2008 [eluser]xwero[/eluser] As an alternative you could use prefix variables in your model. Code: class Mymodel extends Model Code: $this->db->select('t1.username, t2.name, t3.amount'); |