Using Multiple Contexts

A Context is an environment that deals with every persistance engines used by koha. It opens and maintains connexions to DBMS and zebra server relying to your ILS configuration (./koha-conf.xml by default). Many functions of the koha API uses a context set as default (stored in $C4::Context::context).

use C4::Context;

Executes the the C4::Context::import function and uses the $KOHA_CONF environment variable to correctly set a default context. You can get around $KOHA_CONF giving a filename by yourself.

use C4::Contexts '/home/koha/$_/etc/koha-conf.xml';

But what if you don't want to set a context at start? What if you want to connect to two koha bases in the same code? DIY: create your own context using C4::Context::new

use strict;
use warnings;
# where is koha ? 
use lib '/usr/local/src/koha';

# just load C4::Context, don't create a default context
require C4::Context;

# create a function to easily switch context used by koha API.
sub with { $C4::Context::context = shift } 

# create contexts
my %context;
for (qw( example1 example2 )) {
    $context{$_} = C4::Context->new("/home/koha/$_/etc/koha-conf.xml");
}

# use contexts

for (qw( example1 example2 )) {
    with($context{$_});
    print "1st element of $_:\n", GetMarcBiblio(1)->as_formated;
}
 
en/development/usingcontext.txt · Last modified: 2008/08/04 02:08 by hdl
 
Except where otherwise noted, content on this wiki is licensed under the following license:CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki