ORNA的Kohana数据库配置设置

Sha*_*ean 4 php kohana

如何选择ORM应使用的数据库配置?文档仅提到如何设置配置并在使用纯数据库方法时选择它.不是在使用ORM时.

这是我当前的配置:

调节器

<?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
Run Code Online (Sandbox Code Playgroud)

模型

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

class Model_User extends ORM
{   
    protected $_primary_key = 'UserId';
}
Run Code Online (Sandbox Code Playgroud)

配置(位于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,
    ),
);
Run Code Online (Sandbox Code Playgroud)

我只想让ORM使用local数据库.我该怎么做呢?现在我收到错误:Database_Exception [ 2 ]: mysql_connect(): Access denied for user 'www-data'@'localhost' (using password: NO)

Dar*_*tar 6

像Kowser所说,对于Database :: instance()使用'local'数据库组返回连接,使用Database :: $ default ='local';

如果要让类使用不是Database :: $ default的特定数据库组.然后在你的类定义中将$ _db_group设置为数据库配置组,如下所示:

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

class Model_User extends ORM
{
    protected $_db_group = 'local';
    protected $_primary_key = 'UserId';
}
Run Code Online (Sandbox Code Playgroud)

这样,您可以将Database :: $ default设置为'remote',并且只有该类的对象才会使用'local'连接.