Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Specify a database connection on a per-model basis by declaring instance variable #209

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -315,14 +315,14 @@ Database Connection

The class will automatically use the default database connection, and even load it for you if you haven't yet.

You can specify a database connection on a per-model basis by declaring the _$\_db\_group_ instance variable. This is equivalent to calling `$this->db->database($this->_db_group, TRUE)`.
You can specify a database connection on a per-model basis by declaring the _$\_database_ instance variable. This is equivalent to calling `$this->db->database($this->_database, TRUE)`.

See ["Connecting to your Database"](http://ellislab.com/codeigniter/user-guide/database/connecting.html) for more information.

```php
class Post_model extends MY_Model
{
public $_db_group = 'group_name';
public $_database = 'group_name';
}
```

Expand Down
28 changes: 26 additions & 2 deletions core/MY_Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ class MY_Model extends CI_Model
protected $_table;

/**
* The database connection object. Will be set to the default
* connection. This allows individual models to use different DBs
* The database connection object. Will be set to the default group
* connection. This allows individual models to use different DB groups
* without overwriting CI's global $this->db connection.
*/
public $_database;
Expand Down Expand Up @@ -105,6 +105,9 @@ public function __construct()

$this->_fetch_table();

if($this->_database){
$this->db = $this->load->database($this->_database, TRUE);
}
$this->_database = $this->db;

array_unshift($this->before_create, 'protect_attributes');
Expand Down Expand Up @@ -760,6 +763,27 @@ public function order_by($criteria, $order = 'ASC')
return $this;
}

/* --------------------------------------------------------------
* QUERY BUILDER DIRECT ACCESS METHODS
* ------------------------------------------------------------ */
/**
* A wrapper to $this->_database->group_by()
*/
public function group_by($criteria)
{
if ( is_array($criteria) )
{
foreach ($criteria as $key => $value)
{
$this->_database->group_by($value);
}
}
else
{
$this->_database->group_by($criteria);
}
return $this;
}
/**
* A wrapper to $this->_database->limit()
*/
Expand Down