Welcome Guest, Not a member yet? Register   Sign In
how to use dbprefix
#1

[eluser]jorne[/eluser]
Hello everybody, this is my first post here in the forum.

I have been playing around with Codelgniter for a week, and been able to find answers in the documentation and here in the forum until now.

I have this but the database prefix get ignored by Codelgniter:

function index()
{
$this->db->dbprefix('site1_');
$this->db->where('ID',3);
$data['query']= $this->db->get('posts');
$this->load->view('home',$data);
}

do I have to change something In the database.php or config file?
#2

[eluser]Colin Williams[/eluser]
The dbprefix method just returns a string. It does not alter your queries. It would be better to set a default prefix in config/database.php (http://ellislab.com/codeigniter/user-gui...ation.html), which then prefixes table names automatically when table names are used in Active Record functions

Code:
$this->db->query('SELECT * FROM '. $this->db->dbprefix('posts') .' WHERE "ID" = 3');
// Only need to use dbprefix when manually generating queries
#3

[eluser]jorne[/eluser]
What I'm trying to achieve is to change the prefix depending on the URL
Is this not possible?
#4

[eluser]Colin Williams[/eluser]
1. Create db groups for each setting you need in config/database.php
2. Don't autoload the database class
3. In some bootstrap process (probably a hook is best), initialize the correct group with $this->load->database('group_name'), or pass an array of settings to $this->load->database($settings);

This is all covered here: http://ellislab.com/codeigniter/user-gui...cting.html

Alternatively, in config/database.php, you could check the URI against a set of conditions and change $active_group accordingly. Remember though that you probably don't have/can't get an instance of the CI object, so you'll have to rely on Plain Old PHP to check the URL
#5

[eluser]jorne[/eluser]
Late better then never? Thanks for your reply.




Theme © iAndrew 2016 - Forum software by © MyBB