Hello,
How can I make a CRUD for another table in another database, that is not the "default" ?
⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠
Posted 03 March 2013 - 18:02 PM
Hello,
How can I make a CRUD for another table in another database, that is not the "default" ?
Posted 03 March 2013 - 23:39 PM
Hi Smilie and welcome to the forum.
You can create oun model and set it instead the default model.
more info here:
http://www.grocerycrud.com/documentation/options_functions/set_model
Posted 04 March 2013 - 07:17 AM
Hi,
Thanks :)
I don't quite get it, I have to override all the functions, that I need to use? Isn't there an easier way to do this? :p
Posted 04 March 2013 - 11:07 AM
1. on application/config/database.php put this code
$db['db2']['hostname'] = 'localhost';
$db['db2']['username'] = 'root';
$db['db2']['password'] = 'password';
$db['db2']['database'] = 'database';
$db['db2']['dbdriver'] = 'mysql';
$db['db2']['dbprefix'] = '';
$db['db2']['pconnect'] = TRUE;
$db['db2']['db_debug'] = TRUE;
$db['db2']['cache_on'] = FALSE;
$db['db2']['cachedir'] = '';
$db['db2']['char_set'] = 'utf8';
$db['db2']['dbcollat'] = 'utf8_general_ci';
$db['db2']['swap_pre'] = '';
$db['db2']['autoinit'] = TRUE;
$db['db2']['stricton'] = FALSE;
Check here, http://ellislab.com/codeigniter/user-guide/database/configuration.html
2. on your controller change your constuction like this,
Check here, http://ellislab.com/codeigniter/user-guide/database/connecting.html
Posted 04 March 2013 - 18:12 PM
This wont change the db used by the lib :( as db is in autoload.
I was hoping that I could simply specify what connection to use like
$DB1 = $this->load->database('group_one', TRUE);
and then pass $DB1 as a parameter, and it would use that.
Posted 05 March 2013 - 01:07 AM
You must remember that your controller is an object oriented class.
In one function (method) you can load default database and use that connection and its tables and on another function you
could load a db2 config group and load it.
Grocery CRUD uses the active connection when it loads.
The only time that may arise a problem is when you want under the same function to get two (or more) db connections.
For this I can not answer for the moment, not while the core developer of the library builds the master/detail feature of the library.
I hope it makes sense all this.
It is just OO concept under PHP/CI.
Posted 05 March 2013 - 06:37 AM
Yes it makes sense,
I tried to
$this->db->close();
$this->load->database('market');
But for whatever reason, I'm still on the default connection, not the market group.
Posted 05 March 2013 - 09:38 AM
When I have some time I 'll try to make an example.
Posted 06 March 2013 - 11:39 AM
for eaxmple if you want to connect another database say 'mssql'
1. In application/config/database.php, put this lines
$db['mssql']['hostname'] = 'hostname';
$db['mssql']['username'] = 'username';
$db['mssql']['password'] = 'password';
$db['mssql']['database'] = 'db_name';
$db['mssql']['dbdriver'] = 'mssql';
$db['mssql']['dbprefix'] = '';
$db['mssql']['pconnect'] = TRUE;
$db['mssql']['db_debug'] = TRUE;
$db['mssql']['cache_on'] = FALSE;
$db['mssql']['cachedir'] = '';
$db['mssql']['char_set'] = 'utf8';
$db['mssql']['dbcollat'] = 'utf8_general_ci';
$db['mssql']['swap_pre'] = '';
$db['mssql']['autoinit'] = TRUE;
$db['mssql']['stricton'] = FALSE;
2. in your model
function __construct()
{
$this->mssql = $this->load->database('mssql', TRUE);
parent::__construct();
}
3. your query will be as follows
function get_data()
{
$query=$this->mssql->select('*');
$query=$this->mssql->from($table);
$query=$this->mssql->where($where);
$query=$this->mssql->order_by($orderby,'ASC');
$query=$this->mssql->get();
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row;
}
return $data;
}
return false;
}