Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kohana database config settings for ORM

Tags:

php

kohana

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)

like image 533
Shawn Mclean Avatar asked Feb 23 '23 07:02

Shawn Mclean


1 Answers

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.

like image 167
Darsstar Avatar answered Mar 06 '23 16:03

Darsstar