Kohana Database Configuration Settings for ORM

How to choose the database configuration that ORM should use? The docs mention how to configure and select it using the clean database method. Not when using ORM.

Here is my current configuration:

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'; } 

Configuration (this is in the app /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 ORM to use the local database. How can I do it? Now I get the error message: Database_Exception [ 2 ]: mysql_connect(): Access denied for user 'www-data'@'localhost' (using password: NO)

+4
source share
2 answers

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

If you want the class to use a specific database group that is not base :: $ default. Then in your class definition, set $ _db_group to the database configuration group as follows:

 <?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 this class will use the local connection.

+6
source

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

 Database::$default = "local"; 

That should do the trick.

+2
source

Source: https://habr.com/ru/post/1383452/


All Articles