Search code examples
phpmysqlcodeigniteractiverecordcodeigniter-2

In Codeingiter db active dbprefix don't apply in concat(), sum() methods


I am using codeigniter 2.2.4

I have to use table prefixes for backups every year i.e 2017_

It does n't work in complex select queries like

$this->db->select('concat(users.firstname, " ", users.lastname) AS name users.email', FALSE);
$this->db->get('users');

echo $this->db->last_query();
die();

The above code returns this query

SELECT concat(users.firstname, ' ', 2017_users.lastname) AS name, 2017_users.email FROM (`2017_users`)

I want to know if there is any other way to use concat(), sum() in codeigniter DB active record so that it can apply db_prefixes

I want this result when I set codeigniter dbprefix to 2017_

SELECT concat(2017_users.firstname, ' ', 2017_users.lastname) AS name, 2017_users.email FROM (`2017_users`)

Also add prefix in table names that comes in sql functions i.e concat(), sum()


Solution

  • You can append prefix by $this->db->dbprefix. change your code to:

    $this->db->select("concat({$this->db->dbprefix}users.firstname, ' ', 
    {$this->db->dbprefix}users.lastname) AS name, users.email", FALSE);
    $this->db->get('users');
    
    echo $this->db->last_query();
    die();