How to use an alternate DB connection in Kohana 3.1

If you run the next bit of code from a Kohana 3.1 controller

$query = DB::select("select * from foo");
$results = $query->execute();
foreach($results as $result)
{
    var_dump($result);
}

Kohana will try to connect to the database using information from the array returned application/config/database.php. In particular, if you will use the information specified in the default group .

return array
(
    'default' => array
    (
        'type'       => 'mysql',
        'connection' => array(
            /**
             * The following options are available for MySQL:
             *
             * string   hostname     server hostname, or socket
             * string   database     database name
             * string   username     database username
             * string   password     database password
             * boolean  persistent   use persistent connections?
             *
             * Ports and sockets may be appended to the hostname.
             */
            'hostname'   => 'localhost',
            'database'   => 'kohana',
            'username'   => FALSE,
            'password'   => FALSE,
            'persistent' => FALSE,

However, this configuration array accepts a few top-level elements (I think this is called db groups). How should I / should tell Kohona 3.1 to connect and query using the information set in a non-default db group.

+3
source share
1 answer

You can pass a database group as an argument execute

: 201 /kohana/database/query.php :: ()

$this->execute('group');

, $query = Database::instance('group')

+6

All Articles