Code igniter prepending db prefix in table aliases

Jatin Dhoot picture Jatin Dhoot · May 11, 2012 · Viewed 11.2k times · Source

I have configured code igniter to use db prefix.

At all other places it is working as expected but while creating table aliases it is prepending db prefix.

Code is as under:-

$this->db->from('table_a');
$this->db->join('table_b', 'table_a.id = table_b.a_id', 'left');
-----
$this->db->join('table_b as tablebAlias', 'table_c.id = tablebAlias.a_id', 'left');

Assuming my dbprefix is set to value 'foo'.

Final query which is getting executed is as under:-

Select * From foo_table_a left join foo_table_b on foo_table_a.id = foo_table_b.a_id
--- left join foo_table_b as tablebAlias on foo_table_c.id = foo_tablebAlias.a_id

Any help will be highly appreciable.

Thanks, Jatin

Answer

Alfi Rizka picture Alfi Rizka · May 23, 2012

I found out that manual queries ignore table prefix. I also found out that there is a way to add table prefix to manual queries:

In config/database.php

One can do this:

$db['default']['dbprefix'] = "feed_";  
$db['default']['swap_pre'] = "{PRE}";

So that this can be done:

$sql = "SELECT * FROM {PRE}item";  
$query = $this->db->query($sql); 

The {PRE} becomes feed_.

But swap_pre is not config.php which leads me to think that this is CI 2.0 feature.