Shawn Mclean
Shawn Mclean

Reputation: 57469

Kohana database config settings for ORM

How do I select which database config my ORM should use? The docs only mentions how to setup the config and select it when using the pure database method. Not when using an ORM.

Here is my current config:

controller

<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Welcome extends Controller {

    public function action_index()
    {
        $members = ORM::factory('user');
        $members->where('first_name', '=', 'Peter')->find_all();
        $memCount = $members->count_all();          
        $this->response->body('Count: ' . $memCount);
    }

} // End Welcome

Model

<?php defined('SYSPATH') or die('No direct access allowed.');

class Model_User extends ORM
{   
    protected $_primary_key = 'UserId';
}

Config (This is located in application/config/database.php

<?php defined('SYSPATH') or die('No direct access allowed.');

return array
(
    'local' => array
    (
        'type'       => 'mysql',
        'connection' => array(
            'hostname'   => 'localhost',
            'database'   => 'dbname',
            'username'   => 'username',
            'password'   => '*******',
            'persistent' => FALSE,
        ),
        'table_prefix' => '',
        'charset'      => 'utf8',
        'caching'      => FALSE,
        'profiling'    => TRUE,
    ),
    'remote' => array(
        'type'       => 'pdo',
        'connection' => array(
            'dsn'        => 'mysql:host=localhost;dbname=kohana',
            'username'   => 'root',
            'password'   => '***',
            'persistent' => FALSE,
        ),
        'table_prefix' => '',
        'charset'      => 'utf8',
        'caching'      => FALSE,
        'profiling'    => TRUE,
    ),
);

I just want the ORM to use the local database. How do I do this? Right now I get the error: Database_Exception [ 2 ]: mysql_connect(): Access denied for user 'www-data'@'localhost' (using password: NO)

Upvotes: 4

Views: 5902

Answers (2)

Darsstar
Darsstar

Reputation: 1895

Like Kowser said, for Database::instance() to return a connection using the 'local' database group use Database::$default = 'local';

If you want to let a class use a specific database group that is not Database::$default. Then in your class definition set $_db_group to the database config group like so:

<?php defined('SYSPATH') or die('No direct access allowed.');

class Model_User extends ORM
{
    protected $_db_group = 'local';
    protected $_primary_key = 'UserId';
}

This way you can set Database::$default to 'remote' and only objects of that class will use the 'local' connection.

Upvotes: 6

Kowser
Kowser

Reputation: 8261

Possibly you know that, Kohana uses default by default. If you want something different by default, add this line to your bootstrap.php:

Database::$default = "local";

This should do the trick.

Upvotes: 2

Related Questions